summaryrefslogtreecommitdiff
path: root/Core/EM/SecurityPkg/ImageVerificationLib/DxeImageVerificationLib.c
blob: 7593b207a46c52ca8ccaee54665ab26847f0b990 (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
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2013, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SecureBoot_WIN8/ImageVerification_efi/DxeImageVerificationLib.c 79    6/22/15 6:26p Alexp $
//
// $Revision: 79 $
//
// $Date: 6/22/15 6:26p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SecureBoot_WIN8/ImageVerification_efi/DxeImageVerificationLib.c $
// 
// 79    6/22/15 6:26p Alexp
// EIP219415
// [Category]  Bug Fix
// [Severity]	 Minor
// [Symptom] UEFI PXE can launch unsigned images when 
//                  SecureBoot is enabled.
// [RootCause]  Image from PXE server gets cloned device path 
//            from its parent network controller with PciIo protocol 
//            and embedded PCI ROM attributes
//  [Solution]  Add checking for unique path for embedded 
//                  Pci OpROMs to prevent non-OpROM images
// 
// 77    5/14/15 9:49a Alexp
// DxeImageVerificationHandler()
//  Modify certificate offset calculation to match EDKII version
// 
// 76    3/09/15 4:24p Alexp
// EIP192504: Prevent Built In EFI Shell boot option from being created in
// BDS when Secure Boot is enabled
// [Resolution]: Leave old logic to allow launching embedded shell 
//  based on the token:LOAD_UNSIGNED_EMBEDDED_SHELL
//   enabled by default only in Debug mode
// EIP197749: Need to authenticate images loaded from external FV
// [Resolution]  Add logic inside GetImageType() to detect images loaded
// from external FV
// 
// 75    5/29/14 8:52a Alexp
// minor debug trace message change;
// 
// 74    8/15/13 11:29a Alexp
// EIP#118850: Implement support for Certificate Revocation Storage
// reduction and Timestamp Support.
// Link AmyCryptoLib to make use of os_mktime()
// 
// 73    7/26/13 3:40p Alexp
// 1. EIP118850: Integrate ECR1009 chnages for TimeStamp dbt processing
// 2. Image policy settings are checked against the build time defaults. 
// Prevents un-authorized change to NVRAM policy settings 
// to lower security policy.
// 
// 72    6/26/13 10:48a Alexp
// EIP[125931]: Security review. Fix after follow up review.  
// UINT32 integer wrapping still occurs due to incorrect placement 
// of delimiting parentheses
// 
// 70    6/25/13 7:21p Alexp
// EIP:127292 Item#2. Add dependency on ENABLE_IMAGE_EXEC_POLICY_OVERRIDE
// EIP:127292 Item#3 Only For deprecated Security protocol:
//  DxeImageVerificationHandler Returns Error if FileRead() is
// unsuccessful
// 
// 67    6/21/13 10:41a Alexp
// EIP[125931]: Security review: HashPeImage does not validate certificate
// table offset and size
// 
// 66    5/21/13 4:05p Alexp
// EIP:124444  Secure boot improvement for LoadImage call with NULL
// DevicePath
// EIP:122339  GetImageType() detect if PciOpROM image is loaded from
// internal FV by
//    checking the corresponding PCI I/O instance for the embedded
// attribute.
// 
// 63    3/25/13 3:41p Alexp
// 1. EIP:118243 add support for multi-signed PE Images
// 2. Removed append image certificates to db from User Query dialog.
// 
// 56    3/11/13 5:07p Alexp
// IsSignatureFoundInDatabase(): fix method to calculate CertCount.
// AddImageExeInfo(): code cleanup for cases when *Name is NULL
// DxeImageVerificationHandler(): Optimized code to determine if PE image
// is signed
// 
// 55    3/09/13 4:31p Alexp
// EIP114998: wrong cert size passed in PCR[7] calculation
// 
// 54    12/19/12 10:28a Alexp
// EIP[104046]: Hardened code per #2,3,4,5,6,7 of security review findings
//  
// 
// 53    12/17/12 3:10p Alexp
// code fixes according to "cppcheck" style & performance suggestions
// 
// 52    12/06/12 7:31p Alexp
// Update ImageAuthorization()
// 
// 51    11/26/12 10:46a Alexp
// replaced hardwired Var name L"SecureBootSetup" to generic Var define
// 
// 50    11/19/12 4:42p Alexp
// Fix for Win8 SecureBoot logo requirement: restore Secure Boot state
// across flash updates.
// Move all secure boot Setup settings to a separate varsore variable. 
// Preserve var across re-flash
// 
// 49    11/09/12 4:32p Alexp
// IsPkcsSignedDataVerifiedBySignatureList()
// Fix CertData size - excude EFI_GUID size
// 
// 48    10/17/12 3:03p Alexp
// EIP[104046]: Findings from Security review on Aptio4 Image verification
//  Includes the fix for item #4:
//  HashPeImage returns TRUE even in error case
// 
// 47    10/16/12 3:53p Alexp
// EIP[104046]: Findings from Security review on Aptio4 Image verification
// Includes the fix for item #7 from the list attached to the EIP
// 
// 46    9/25/12 11:22a Alexp
// removed ImageVerify simulation when system in SetupMode.
// 
// 45    9/24/12 12:26p Alexp
// fix code branch for WIN_CERT_TYPE_EFI_GUID certificates
// 
// 44    9/19/12 4:07p Alexp
// fix code branch to set action EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED
// 
// 43    9/14/12 3:42p Alexp
// Hardwire LoadFromFv to AlwaysEnable irrespective of SetupData
// 
// 42    9/13/12 11:38a Alexp
// Fix Bug in HashPeiImage. 
// Last step 16 in hashing of optional data structures after the
// Certificate entry was missing
// 
// 41    9/07/12 9:37a Alexp
// 1. Set all global variables Static
// 2. IsPkcsSignedDataVerifiedBySignatureList(). Compiler optimization for
//  "IsVerified = VerifyWinCertificateForPkcsSignedData(
//   &(PkcsCertData->Hdr));"
//  may not link the main code.
// 3. Run Verify code when platform is in SetupMode and SecureBoot set to
//     Custom
// 
// 39    9/05/12 5:42p Alexp
// GetImageType() : 
// Change Device Path search criteria to match EDKII example: 
//  check FV path first and then Block I/O
// 
// 38    8/29/12 11:16a Alexp
// Add definition for EFI_MAX_ADDRESS if it's not defined elsewhere in
// included header files
// 
// 37    8/28/12 4:21p Alexp
// Restore dependencies on SecureBootMode->Custom Setup option
// 
// 35    8/15/12 4:16p Alexp
// IsPkcsSignedDataVerifiedBySignatureList()
// Fix bug prev version in processing multiple Certs from PE Hdr.
// 
// 34    7/26/12 3:54p Alexp
// Query User -> Append Certificate only available in Setup Mode.
// 
// 33    7/25/12 6:45p Alexp
// 1. Update ImageVerification logic to the latest EDK2 patch (13469)
//     Add support for multiple Signatures within PE/COFF table
//     Additional Header validation checks 
// 2. Add code flow when SecureBoot set to Custom mode.
//    Image Verification is performed but failing images are allowed to
// run
//    Use this mode to append image certificates to DB in User Query mode.
// 
// 32    6/11/12 3:15p Alexp
//  use modified WIN_CERTIFICATE_UEFI_GUID_1 struc in
//  AppendEfiImageSecurityDatabaseVariableEx()  
//  moded struc is shorter by 4 bytes
// 
// 30    5/18/12 9:21a Alexp
// Fine tune User Query messages. Only display the messages if Admin user
// has signed in   
// 
// 29    5/01/12 4:43p Alexp
// PCR[7] - mTrustSigDbOffs contains offset to SigData struct within SigDb
// 
// 28    4/27/12 3:57p Alexp
// Fix Certificate type in AppendEfiImageSecurityDatabaseVariableEx()
// 
// 27    4/23/12 5:19p Alexp
// EIP:80874 Made changes to support PCR[7] measurments for Secure Boot
// state in TCG
// Install "Efi Boot Image Certificate" info strusture on Efi System
// Table.
// 
// 26    4/20/12 5:14p Alexp
// Add new function to install the handle on Efi System Table with the
// location 
// within DB variable of the Trusted Certificate that was used to verify
// signature of Efi OS BootLoader image.
// 
// 25    4/10/12 6:52p Alexp
// Update VerifyCertPkcsSignedData() to support extended search in
// Forbidded db for any leaf certificate, not only Root CA
// 
// 23    4/03/12 7:35p Alexp
// Move ReadImage() under legacy SecureFileAuthentication()
// 
// 22    4/02/12 4:24p Alexp
// Installs EFI_SECURITY2_ARCH_PROTOCOL. 
// Input arguments include File Path and pointer to a File buffer in
// memory.
// 
// 20    3/20/12 10:51a Alexp
// 1. fix in AppendEfiImageSecurityDatabaseVariableEx()
// 2. removed unused global vars
// 
// 18    3/16/12 10:29a Alexp
// [EIP85500] Build problem with (INT)4.6.5.1_SECUREBOOT_WIN8_11
// move "extern EFI_GUID BdsConnectDriversProtocolGuid " outside of #ifdef
// scope
// 
// 17    3/13/12 2:41p Alexp
// Add check for Admin signed credentials while allowing User Querry
// policy
// 
// 16    3/12/12 3:01p Alexp
// 1. Deffer image verification till BdsConnectDriversProtocol event.
// Speeds up the boot time.
// 2. Enforce default (hardwired by SDL token) image veriication policy
// for image device path if SecureBootMode is set to Standard.
// 
// 15    3/09/12 3:39p Alexp
// 1. Fixed Image authentication by Hash certificate in "db"
// 2. Add User interructive controls to append image certificate to Sig
// "db" (test implementation, will change in later releases)
// 
// 14    2/27/12 6:47p Alexp
// Reduce time spent in the DxeImageVerificationHandler function during
// non-Secure Boot mode. Original code caused additional 0.5 sec to boot
// time.
// Note: internal SecureBoot mode flag once registered during
// initializaton - will not change until restart. 
// 
// 13    1/04/12 3:27p Alexp
// Fixed possible security risk:
// GetImageType(). Block I/O Device Path check made higher priority then
// of FV device path. 
// 
// 12    12/29/11 6:49p Alexp
// GetImageType() - Fix for Image Device Path resolution
// Added SubType = MEDIA_FV_FILEPATH_DP to search options for
// MEDIA_DEVICE_PATH.
// It appears SmmDriverDispatcher sets this device path for files loaded
// by SmmDispatcher.
// 
// 11    12/23/11 8:59a Alexp
// Bug fix:
// EIP:78978:Secure boot will hang at CP 0x72
// ConOut & ConIn are not available until later in BDS phase, earlier
// calls to display User Prompt may hang the system
// 
// 10    12/07/11 7:23p Alexp
// Bug fix:
// EIP:77088  SecureBoot: Image can be loaded if click "No" when query
// user after image verification failed
// ImageAuthorization() -> Return EFI_ACCESS_DENIED of User Override
// option
// 
// 9     8/22/11 11:14a Alexp
// optimizations to GetImageType()
// 
// 6     7/18/11 4:12p Alexp
// EIP:65085. LoadImage Error status is different When Secure Boot is ON
// EIP:65194: No way to control the Secure Boot module to not interact
// with the USER without changing the code
// 
// 4     6/23/11 9:27a Alexp
// moved mDigitalSig Protocol check up before API may be called
// 
// 3     6/22/11 5:45p Alexp
// draft change: add Sha256 as valid cert for "db" databases
// 
// 2     6/13/11 7:21p Alexp
// fix Hash calculation for Sha256 Certs in Forbidden database dbx
// 
// 10    5/17/11 5:36p Alexp
// update conOut messages
// 
// 9     5/17/11 12:50p Alexp
// replace AmiPostManager with pST->ConOut
// use pBS instead of gBS
// 
// 6     5/13/11 4:02p Alexp
//  1. add dependency on Core rev. 4.6.5+ to buld for older Aptio cores
//  2. Ignore Querry User policy for internal FV files
// 
//**********************************************************************
/*++
  This file contains an 'Intel Peripheral Driver' and is        
  licensed for Intel CPUs and chipsets under the terms of your  
  license agreement with Intel or your vendor.  This file may   
  be modified by the user, subject to additional terms of the   
  license agreement                                             
--*/
/** @file

  Implement image verification services for secure boot
  service in UEFI2.2.

  Caution: This file requires additional review when modified.
  This library will have external input - PE/COFF image.
  This external input must be validated carefully to avoid security issue like
  buffer overflow, integer overflow.

  DxeImageVerificationLibImageRead() function will make sure the PE/COFF image content
  read is within the image buffer.

  DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept
  untrusted PE/COFF image and validate its data structure within this image buffer before use.

Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>

This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.

**/

#pragma warning (disable : 4090)

#include <Token.h>
#include <Protocol/Security.h>
#include "DxeImageVerificationLib.h"
#include "EfiImage.h"
#include <AmiDxeLib.h>
#include<Guid/PeiPeCoffLoader.h>
#include <Protocol/AmiDigitalSignature.h>
#include <AmiCertificate.h>
#include <Setup.h>
#include <SecureBootMod.h>
#include <Protocol/PciIo.h> //EIP 122339
#include <cryptlib.h>

extern EFI_GUID BdsConnectDriversProtocolGuid;

// 4.6.5.4
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
#include <Protocol/Security2.h>
#endif

// 4.6.5.1
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028b
#include <EdkIICommon.h>
#else
#include <Tiano.h>

//********tmp declaration from EdkIICommon.h
#define MEDIA_RELATIVE_OFFSET_RANGE_DP 0x08

extern VOID ZeroMem (
    OUT VOID    *Buffer,
    IN  UINTN   Size 
);
VOID* CopyMem (
    OUT VOID  *DestinationBuffer,
    IN  VOID  *SourceBuffer,
    IN UINTN  Length
);
#endif
EFI_STATUS
EFIAPI
PeCoffLoaderGetImageInfo (
  IN     EFI_PEI_PE_COFF_LOADER_PROTOCOL       *This,
  IN OUT EFI_PEI_PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
  );

#include <Protocol/SimpleTextIn.h>
#include <Protocol/SimpleTextOut.h>

#define AMI_MEDIA_DEVICE_PATH_GUID \
    { 0x5023b95c, 0xdb26, 0x429b, 0xa6, 0x48, 0xbd, 0x47, 0x66, 0x4c, 0x80, 0x12 }

#define STANDARD_SECURE_BOOT       0
#define CUSTOM_SECURE_BOOT         1

static EFI_GUID AmiMediaDevicePathGuid  = AMI_MEDIA_DEVICE_PATH_GUID;
static EFI_GUID gSecureSetupGuid        = SECURITY_FORM_SET_GUID;
static EFI_GUID gEfiGlobalVariableGuid  = EFI_GLOBAL_VARIABLE;

static AMI_DIGITAL_SIGNATURE_PROTOCOL *mDigitalSigProtocol = NULL;
//
// Caution: This is used by a function which may receive untrusted input.
// These global variables hold PE/COFF image data, and they should be validated before use.
//
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION        mNtHeader;
static UINT8                              *mImageBase = NULL;
static UINTN                               mImageSize;
static EFI_GUID                           *mCertType;
static UINT32                              mPeCoffHeaderOffset; 
static UINT8                               mImageDigest[MAX_DIGEST_SIZE];
static UINTN                               mImageDigestSize;
static SECURE_BOOT_SETUP_VAR               mSecureBootSetup;
static UINT8                               mSecureBootEnable = 0;
static UINTN                               mFvHandlesCount   = 0;
static EFI_HANDLE                          *mFvHandles       = NULL;
static BOOLEAN                             gImageLoadingAfterBDS = FALSE;

//
// Notify string for authorization UI.
//
CHAR16  mNotifyString1[MAX_NOTIFY_STRING_LEN] = L"\r\nImage verification failed!\r\n";
CHAR16  mNotifyString2[MAX_NOTIFY_STRING_LEN] = L"\r\nLaunch this image anyway? (Y/N)";
CHAR16  mNotifyString3[MAX_NOTIFY_STRING_LEN] = L"Image Certificate not found in Authorized database(db)!";
CHAR16  mNotifyString4[MAX_NOTIFY_STRING_LEN] = L"Image Certificate is found in Forbidden database(dbx)!";
CHAR16  mNotifyString5[MAX_NOTIFY_STRING_LEN] = L"Image is unsigned or Certificate is invalid!";

//6683D10C-CF6E-4914-B5B4-AB8ED7370ED7
EFI_GUID gBootImageCertTblGuid  = AMI_VALID_BOOT_IMAGE_CERT_TBL_GUID;
static UINT32   mTrustSigDbOffs = 0;
static UINT32   mTrustSigDbSize = 0;

//---------------------------------------
// NEW TIME STAMP definitions ECR#1009
//---------------------------------------
extern EFI_GUID gEfiCertX509Sha256Guid;
extern EFI_GUID gEfiCertX509Sha384Guid;
extern EFI_GUID gEfiCertX509Sha512Guid;
static INT32     mTimeOfSigningLong;
typedef enum {
    CertUndefined,
    CertSha256,
    CertX509, 
    CertX509Sha256, 
    CertX509Sha384, 
    CertX509Sha512, 
} nCertType;
//---------------------------------------
// NEW TIME STAMP definitions ECR#1009
//---------------------------------------

//************TEMP UTILITY FUNCTIONS. Use EDKII common wrapper lib instead****************
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028b
#else
UINTN StrSize (
    IN CHAR16 *String)
{
  UINTN Size;

  for (Size = 2; *String != L'\0'; String++, Size += 2);

  return Size;
}
#endif

VOID* GetEfiGlobalVariableEx (
    IN CHAR16 *Name,
    IN OUT UINTN *VarSize
    )
{
    EFI_STATUS Status;
    VOID *Var=NULL;

    Status = GetEfiVariable(Name, &gEfiGlobalVariableGuid, NULL, VarSize, &Var);
    return (EFI_ERROR(Status)) ? NULL : Var;
}

VOID* GetEfiImageSecurityDatabaseVariableEx (
    IN CHAR16 *Name,
    IN OUT UINTN *VarSize
    )
{
    EFI_STATUS Status;
    VOID *Var = NULL;

    Status = GetEfiVariable(Name, &gEfiImageSecurityDatabaseGuid, NULL, VarSize, &Var);
    return (EFI_ERROR(Status)) ? NULL : Var;
}

/**
  Reads contents of a PE/COFF image in memory buffer.

  Caution: This function may receive untrusted input.
  PE/COFF image is external input, so this function will make sure the PE/COFF image content
  read is within the image buffer.

  @param  FileHandle      Pointer to the file handle to read the PE/COFF image.
  @param  FileOffset      Offset into the PE/COFF image to begin the read operation.
  @param  ReadSize        On input, the size in bytes of the requested read operation.  
                          On output, the number of bytes actually read.
  @param  Buffer          Output buffer that contains the data read from the PE/COFF image.
  
  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size 
**/
EFI_STATUS
EFIAPI
DxeImageVerificationLibImageRead (
  IN     VOID    *FileHandle,
  IN     UINTN   FileOffset,
  IN OUT UINTN   *ReadSize,
  OUT    VOID    *Buffer
  )
{

  if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {
    return EFI_INVALID_PARAMETER;    
  }

  if ((EFI_MAX_ADDRESS - FileOffset) < *ReadSize) {
    return EFI_INVALID_PARAMETER;
  }

  if (((UINT64)FileOffset + *ReadSize) > mImageSize) {
//    *ReadSize = (UINT32)(mImageSize - FileOffset);
    return EFI_INVALID_PARAMETER;
  }

  if (FileOffset >= mImageSize) {
//    *ReadSize = 0;
    return EFI_INVALID_PARAMETER;
  }

  CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);

  return EFI_SUCCESS;
}


/**
  Get the image type.

  @param[in]    File       This is a pointer to the device path of the file that is
                           being dispatched. 

  @return UINT32           Image Type

**/
UINT32
GetImageType (
  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File
  )
{
  EFI_STATUS                        Status;
  EFI_HANDLE                        DeviceHandle; 
  EFI_DEVICE_PATH_PROTOCOL          *TempDevicePath;
  EFI_BLOCK_IO_PROTOCOL             *BlockIo;
  UINT8                             nDevicePathSubType;
  //EIP 122339 Start
  EFI_PCI_IO_PROTOCOL               *PciIoInterface = NULL;
  UINT64                            AttributesResult;
  //EIP 122339 Stop
  UINTN                             Index;
  BOOLEAN                           IsFv = FALSE;

  // Unknown device path: image security policy is applied to the image with the least trusted origin.
  if (File == NULL) {
   return IMAGE_UNKNOWN;
  }

  //
  // Check if File is just a Firmware Volume.
  //
  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)File;
  while (!IsDevicePathEndType(TempDevicePath)){
      nDevicePathSubType = DevicePathSubType (TempDevicePath);
      TRACE((TRACE_ALWAYS,"Device PathType %x, SubType %x\n", DevicePathType(TempDevicePath), nDevicePathSubType ));
    //
    // EFI Specification extension on Media Device Path. MEDIA_FW_VOL_FILEPATH_DEVICE_PATH is adopted by UEFI later and added in UEFI2.10. 
    // In EdkCompatibility Package, we only support MEDIA_FW_VOL_FILEPATH_DEVICE_PATH that complies with EFI 1.10 and UEFI 2.10.
    //
    if( (DevicePathType(TempDevicePath) == MEDIA_DEVICE_PATH && nDevicePathSubType == MEDIA_FV_FILEPATH_DP) ||
        (DevicePathType(TempDevicePath) == HARDWARE_DEVICE_PATH && nDevicePathSubType == HW_MEMMAP_DP)
    ){
        IsFv = TRUE;
    } else {
        IsFv = FALSE;
        break;
    }
    TempDevicePath = NextDevicePathNode(TempDevicePath);
  }

  //
  // Check to see if a File or a FV is from internal Firmware Volume.
  //
  if(IsFv) {

      DeviceHandle      = NULL;
      TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)File;
      Status = pBS->LocateDevicePath (
#if (PI_SPECIFICATION_VERSION < 0x00010000)
                      &gEfiFirmwareVolumeProtocolGuid,
#else
                      &gEfiFirmwareVolume2ProtocolGuid,
#endif
                      &TempDevicePath,
                      &DeviceHandle
                      );

    if (!EFI_ERROR (Status)) {
        if(!gImageLoadingAfterBDS)
          return IMAGE_FROM_FV;

        Status = pBS->OpenProtocol (
                        DeviceHandle,
#if (PI_SPECIFICATION_VERSION < 0x00010000)
                       &gEfiFirmwareVolumeProtocolGuid,
#else
                       &gEfiFirmwareVolume2ProtocolGuid,
#endif
                        NULL,
                        NULL,
                        NULL,
                        EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                        );
        if (!EFI_ERROR (Status)) {
            for(Index=0;Index < mFvHandlesCount;Index++){
    TRACE((TRACE_ALWAYS,"FV%02d. DeviceHandle %X=%X\n", Index, DeviceHandle, mFvHandles[Index] ));
                if(DeviceHandle == mFvHandles[Index] )
                    return IMAGE_FROM_FV;
            }
        }
    }
    return IMAGE_UNKNOWN;
  }
  //
  // Next check to see if File is from a Block I/O device
  // Must be a Block I/O device since we reached here after Int FV path check
  //
  DeviceHandle   = NULL;
  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)File;
  Status = pBS->LocateDevicePath (
                  &gEfiBlockIoProtocolGuid,
                  &TempDevicePath,
                  &DeviceHandle
                  );
  if (!EFI_ERROR (Status)) {
    BlockIo = NULL;
    Status = pBS->OpenProtocol (
                    DeviceHandle,
                    &gEfiBlockIoProtocolGuid,
                    (VOID **) &BlockIo,
                    NULL,
                    NULL,
                    EFI_OPEN_PROTOCOL_GET_PROTOCOL
                    );
    if (!EFI_ERROR (Status) && BlockIo != NULL) {
      if (BlockIo->Media != NULL) {
        if (BlockIo->Media->RemovableMedia) {
          //
          // Block I/O is present and specifies the media is removable
          //
          return IMAGE_FROM_REMOVABLE_MEDIA;
        } else {
          //
          // Block I/O is present and specifies the media is not removable
          //
          return IMAGE_FROM_FIXED_MEDIA;
        }
      }
    }
  }
  //
  // File is not in a Firmware Volume or on a Block I/O device, so check to see if 
  // the device path supports the Simple File System Protocol.
  //
  DeviceHandle   = NULL;
  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)File;
  Status = pBS->LocateDevicePath (
                  &gEfiSimpleFileSystemProtocolGuid,
                  &TempDevicePath,
                  &DeviceHandle
                  );
  if (!EFI_ERROR (Status)) {
    //
    // Simple File System is present without Block I/O, so assume media is fixed.
    //
    return IMAGE_FROM_FIXED_MEDIA;
  }

  //
  // File is not from an FV, Block I/O or Simple File System, so the only options
  // left are a PCI Option ROM and a Load File Protocol such as a PXE Boot from a NIC.  
  //
  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)File;
  while (!IsDevicePathEndType (TempDevicePath)) {
    nDevicePathSubType = DevicePathSubType (TempDevicePath);
    switch (DevicePathType (TempDevicePath)) {
    //EIP 219415 Start
    //Embedded OpROM
    case HARDWARE_DEVICE_PATH:

        if(nDevicePathSubType == HW_MEMMAP_DP) {
            //EIP 122339 Start: Return IMAGE_FROM_FV if the EFI_PCI_IO_PROTOCOL installed
            //on the same handle as the Device Path has the attribute EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
            DeviceHandle = NULL;
            //
            // Check if an instance of the EFI_PCI_IO_PROTOCOL is installed on the same handle
            // as the Device Path.  If an instance is found WorkAroundDevHandle contains the
            // handle for the Device Path and EFI_PCI_IO_PROTOCOL instance.
            //
            Status = pBS->LocateDevicePath(
                            &gEfiPciIoProtocolGuid,
                            &File,
                            &DeviceHandle
            );
            if(EFI_ERROR(Status)) break;

            Status = pBS->HandleProtocol(
                            DeviceHandle,
                            &gEfiPciIoProtocolGuid,
                            &PciIoInterface
            );
              
            if(EFI_ERROR(Status)) break;

            //
            // Using the EFI_PCI_IO_PROTOCOL get the value of the PCI controller's
            // Embedded Rom attribute (EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM)
            //
            Status = PciIoInterface->Attributes(
                                    PciIoInterface,
                                    EfiPciIoAttributeOperationGet,
                                    0, //this parameter is ignored during Get operation
                                    &AttributesResult
            );
            if(EFI_ERROR(Status)) break;
            //
            // Check if the PCI controller's EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM is set
            //          
            if(AttributesResult & EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM)
                return IMAGE_FROM_FV;
            //EIP 122339 Stop
        }
        break;
    //EIP 219415 end
    case MEDIA_DEVICE_PATH:

      if (nDevicePathSubType == MEDIA_RELATIVE_OFFSET_RANGE_DP) {
        return IMAGE_FROM_OPTION_ROM;
      }
#if LOAD_UNSIGNED_EMBEDDED_SHELL == 1
      // Case for embedded FV application such as Shell(check GUID. BootOptions.h)
      if (nDevicePathSubType == MEDIA_VENDOR_DP && 
          !guidcmp(&((VENDOR_DEVICE_PATH*)TempDevicePath)->Guid, &AmiMediaDevicePathGuid)) {
            return IMAGE_FROM_FV;
      }
#endif
      break;

    case MESSAGING_DEVICE_PATH:

      if (nDevicePathSubType == MSG_MAC_ADDR_DP) {
        return IMAGE_FROM_REMOVABLE_MEDIA;
      } 
      break;
    }
    TempDevicePath = NextDevicePathNode (TempDevicePath);
  }
  
  return IMAGE_UNKNOWN; 
}

/**
  Caculate hash of Pe/Coff image based on the authenticode image hashing in
  PE/COFF Specification 8.0 Appendix A

  Caution: This function may receive untrusted input.
  PE/COFF image is external input, so this function will validate its data structure
  within this image buffer before use.

  @param[in]    HashAlg   Hash algorithm type.

  @retval TRUE            Successfully hash image.
  @retval FALSE           Fail in hash image.

**/
BOOLEAN
HashPeImage (
  IN  UINT32              HashAlg
  )
{
  EFI_STATUS                EfiStatus;    
  BOOLEAN                   Status;
  UINT16                    Magic;
  EFI_IMAGE_SECTION_HEADER  *Section;
  UINT8                     *HashBase;
  UINTN                     HashSize;
  UINTN                     SumOfBytesHashed;
  EFI_IMAGE_SECTION_HEADER  *SectionHeader;
  UINTN                     Index;
  UINTN                     Pos;

  const UINT8             *addr[MAX_ELEM_NUM]; // tbd. test if 20 elements is enough
  UINTN                   num_elem, len[MAX_ELEM_NUM];
  EFI_GUID                *EfiHashAlgorithmGuid;

  UINT32                    CertSize;
  UINT32                    NumberOfRvaAndSizes;

  SectionHeader = NULL;
  Status        = FALSE;
  EfiStatus     = EFI_SECURITY_VIOLATION;
  num_elem      = 0;
  //
  // Initialize context of hash.
  //
  ZeroMem (mImageDigest, MAX_DIGEST_SIZE);
  if (HashAlg == HASHALG_SHA1) {
    mImageDigestSize     = SHA1_DIGEST_SIZE;
    EfiHashAlgorithmGuid = &gEfiHashAlgorithmSha1Guid;
    mCertType            = &gEfiCertSha1Guid;
  } else if (HashAlg == HASHALG_SHA256) {
    mImageDigestSize     = SHA256_DIGEST_SIZE;
    EfiHashAlgorithmGuid = &gEfiHashAlgorithmSha256Guid;
    mCertType            = &gEfiCertSha256Guid;
  } else {
    return FALSE;
  }

  // 1.  Load the image header into memory.

  //
  // Measuring PE/COFF Image Header;
  // But CheckSum field and SECURITY data directory (certificate) are excluded
  //
  if (mNtHeader.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    //
    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value 
    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the 
    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
    //
    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
  } else {
    //
    // Get the magic value from the PE/COFF Optional Header
    //
    Magic =  mNtHeader.Pe32->OptionalHeader.Magic;
  }
  
  //
  // 3.  Calculate the distance from the base of the image header to the image checksum address.
  // 4.  Hash the image header from its base to beginning of the image checksum.
  //
  HashBase = mImageBase;
  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    //
    // Use PE32 offset.
    //
    HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.CheckSum) - HashBase);
    NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;
  } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
    //
    // Use PE32+ offset.
    //
    HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.CheckSum) - HashBase);
    NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
  } else {
    //
    // Invalid header magic number.
    //
    TRACE((TRACE_ALWAYS,"Invalid header magic number.\n"));
    goto Done;
  }

    if (HashSize != 0) 
    {
           addr[num_elem] = HashBase;
           len[num_elem++] =  HashSize;
    } else {
//TRACE((TRACE_ALWAYS,"Hash1.%x\n", HashSize));
        goto Done;}
  //
  // 5.  Skip over the image checksum (it occupies a single ULONG).
  //
  if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
    //
    // 6.  Since there is no Cert Directory in optional header, hash everything
    //     from the end of the checksum to the end of image header.
    //
    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
      //
      // Use PE32 offset.
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
      HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
    } else {
      //
      // Use PE32+ offset.
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
      HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
    }

    if (HashSize != 0) 
    {
           addr[num_elem] = HashBase;
           len[num_elem++] =  HashSize;
    } else {
//TRACE((TRACE_ALWAYS,"Hash2.%x\n", HashSize));
        goto Done;}
  } else {
    //
    // 7.  Hash everything from the end of the checksum to the start of the Cert Directory.
    //
    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
      //
      // Use PE32 offset.
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
      HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
    } else {
      //
      // Use PE32+ offset.
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
      HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
    }
    if (HashSize != 0) 
    {
           addr[num_elem] = HashBase;
           len[num_elem++] =  HashSize;
    } else {
//TRACE((TRACE_ALWAYS,"Hash3.%x\n", HashSize));
        goto Done;}

    //
    // 8.  Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
    // 9.  Hash everything from the end of the Cert Directory to the end of image header.
    //
    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
      //
      // Use PE32 offset
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
      HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
    } else {
      //
      // Use PE32+ offset.
      //
      HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
      HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
    }
    if (HashSize != 0) 
    {
           addr[num_elem] = HashBase;
           len[num_elem++] =  HashSize;
    } else {
//TRACE((TRACE_ALWAYS,"Hash4.%x\n", HashSize));
        goto Done;}
  }

  //
  // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
  //
  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    //
    // Use PE32 offset.
    //
    SumOfBytesHashed = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders;
  } else {
    //
    // Use PE32+ offset
    //
    SumOfBytesHashed = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders;
  }


  Section = (EFI_IMAGE_SECTION_HEADER *) (
               mImageBase +
               mPeCoffHeaderOffset +
               sizeof (UINT32) +
               sizeof (EFI_IMAGE_FILE_HEADER) +
               mNtHeader.Pe32->FileHeader.SizeOfOptionalHeader
               );
  //
  // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
  //     structures in the image. The 'NumberOfSections' field of the image
  //     header indicates how big the table should be. Do not include any
  //     IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
  //
// TRACE((TRACE_ALWAYS,"Num Sections = %x\n",  mNtHeader.Pe32->FileHeader.NumberOfSections));

// Security review [305408]: HashPeImage does not validate NumberOfSections field
  Pos = sizeof (EFI_IMAGE_SECTION_HEADER) * mNtHeader.Pe32->FileHeader.NumberOfSections;
  if(Pos > mImageSize)
    goto Done;

  EfiStatus = pBS->AllocatePool(EfiBootServicesData, Pos, &SectionHeader);
  if (SectionHeader == NULL || EFI_ERROR(EfiStatus)) {
//TRACE((TRACE_ALWAYS,"Hash4.\n"));
    goto Done;

  }
  MemSet(SectionHeader, Pos, 0);
  //
  // 12.  Using the 'PointerToRawData' in the referenced section headers as
  //      a key, arrange the elements in the table in ascending order. In other
  //      words, sort the section headers according to the disk-file offset of
  //      the section.
  //
  for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {
    Pos = Index;

    // Security review EIP[104046]:6. HashPeImage does not validate NumberOfSections field
    if(((UINT64)Section + sizeof (EFI_IMAGE_SECTION_HEADER))> ((UINT64)mImageBase+mImageSize) )
        goto Done;

    while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {
      CopyMem (&SectionHeader[Pos], &SectionHeader[Pos - 1], sizeof (EFI_IMAGE_SECTION_HEADER));
      Pos--;
    }
    CopyMem (&SectionHeader[Pos], Section, sizeof (EFI_IMAGE_SECTION_HEADER));
    Section += 1;
  }

  //
  // 13.  Walk through the sorted table, bring the corresponding section
  //      into memory, and hash the entire section (using the 'SizeOfRawData'
  //      field in the section header to determine the amount of data to hash).
  // 14.  Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
  // 15.  Repeat steps 13 and 14 for all the sections in the sorted table.
  //
  for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {
    Section = &SectionHeader[Index];
    if (Section->SizeOfRawData == 0) {
      continue;
    }

//TRACE((TRACE_ALWAYS,"Section->PointerToRawData = %x\nSection->SizeOfRawData = %x\nSumOfBytesHashed = %x\n",Section->PointerToRawData, Section->SizeOfRawData, SumOfBytesHashed+Section->SizeOfRawData));
    // Security review EIP[104046]: 5.HashPeImage does not validate PointerToRawData and SizeOfRawData fields
    if( ((UINT64)Section->PointerToRawData + Section->SizeOfRawData) > mImageSize )
        goto Done;

    HashBase  = mImageBase + Section->PointerToRawData;
    HashSize  = (UINTN) Section->SizeOfRawData;

    addr[num_elem] = HashBase;
    len[num_elem++] =  HashSize;
    if(num_elem >= MAX_ELEM_NUM)
//        goto Done;
         {
TRACE((TRACE_ALWAYS,"MAX_ELEM_NUM.1 = %d\n", num_elem));
        goto Done;}
    // Security review EIP[125931]: HashPeImage does not validate certificate table offset and size
    if (((UINT64)SumOfBytesHashed+HashSize) > mImageSize) 
        goto Done;

    SumOfBytesHashed += HashSize;
  }

  //
  // 16.  If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
  //      data in the file that needs to be added to the hash. This data begins
  //      at file offset SUM_OF_BYTES_HASHED and its length is:
  //             FileSize  -  (CertDirectory->Size)
  //
//dbg
//TRACE((TRACE_ALWAYS,"mImageSize > SumOfBytesHashed.\n%x > %x\n", mImageSize, SumOfBytesHashed));
//dbg
  if (mImageSize > SumOfBytesHashed) {

    HashBase = mImageBase + SumOfBytesHashed;

    if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
      CertSize = 0;
    } else {
      if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
        //
        // Use PE32 offset.
        //
        CertSize = mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
      } else {
        //
        // Use PE32+ offset.
        //
        CertSize = mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
      }
    }

    if (mImageSize > ((UINT64)CertSize + SumOfBytesHashed)) {
      HashSize = (UINTN) (mImageSize - CertSize - SumOfBytesHashed);

//TRACE((TRACE_ALWAYS,"\nData beyond nSumOfBytesHashed\nBase = %x, Size = %x\n",SumOfBytesHashed, HashSize));

       addr[num_elem] = HashBase;
       len[num_elem++] =  HashSize;
       if(num_elem >= MAX_ELEM_NUM)
//         goto Done;
         {
TRACE((TRACE_ALWAYS,"MAX_ELEM_NUM.2=%d\n", num_elem));
        goto Done;}

    } else if (mImageSize < ((UINT64)CertSize + SumOfBytesHashed)) {
TRACE((TRACE_ALWAYS,"mImageSize < CertSize + SumOfBytesHashed.\n%d < %d\n", mImageSize, CertSize + SumOfBytesHashed));
          goto Done;
        } 
        else 
            HashSize = 0;
 // ???     HashBase += CertSize;
//TRACE((TRACE_ALWAYS,"\nData beyond CertDir\nBase = %x, Size = %x\n",SumOfBytesHashed+HashSize+CertSize, mImageSize-(SumOfBytesHashed+HashSize+CertSize)));
  }
  EfiStatus = mDigitalSigProtocol->Hash(
              mDigitalSigProtocol, 
              EfiHashAlgorithmGuid, num_elem, addr, len, (UINT8*)&mImageDigest); 

TRACE((TRACE_ALWAYS,"Found HashElements %d\nImageHash: [%2X],[%2X],..\n", num_elem, mImageDigest[0], mImageDigest[1]));

  if(!EFI_ERROR(EfiStatus))
    Status = TRUE;

Done:  

  if (SectionHeader != NULL) {
    pBS->FreePool (SectionHeader);
  }
  // Security review EIP[104046]: 4.HashPeImage returns TRUE even in error case
  // Status = FALSE unless updated to TRUE in the final Hash step before "Done" label
  return Status;
}

/**
  Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of
  Pe/Coff image based on the authenticode image hashing in PE/COFF Specification
  8.0 Appendix A

  Caution: This function may receive untrusted input.
  PE/COFF image is external input, so this function will validate its data structure
  within this image buffer before use.

  @param[in]  AuthData            Pointer to the Authenticode Signature retrieved from signed image.
  @param[in]  AuthDataSize        Size of the Authenticode Signature in bytes.
  
  @retval EFI_UNSUPPORTED             Hash algorithm is not supported.
  @retval EFI_SUCCESS                 Hash successfully.

**/
EFI_STATUS
HashPeImageByType (
  IN UINT8              *AuthData,
  IN UINTN              AuthDataSize
  )
{
  EFI_STATUS                Status;
  UINT8                     DigestAlgo;  
  UINT8                    *pDigestAlgo;
  UINTN                     Size;

// get Digest Algorithm
  Size         = 0;  
  pDigestAlgo  = (UINT8*)&DigestAlgo;
  Status = mDigitalSigProtocol->Pkcs7Verify (
         mDigitalSigProtocol,
         AuthData,
         AuthDataSize,
         NULL,
         0,
         &pDigestAlgo,        // returns DER Ptr to Sign Cert
         &Size,
         Pkcs7GetDigestAlgorithm,
         LOCK
         );
  if (EFI_ERROR(Status)) 
    return Status;

  switch(DigestAlgo) {
    case SHA1:
          if (!HashPeImage (HASHALG_SHA1)) {
              Status = EFI_SECURITY_VIOLATION;
          }
        break;
    case SHA256:
          if (!HashPeImage (HASHALG_SHA256)) {
              Status = EFI_SECURITY_VIOLATION;
          }
        break;
    default:
        Status = EFI_UNSUPPORTED;
        break;
  }

  return Status;
}

/**
  Returns the size of a given image execution info table in bytes.

  This function returns the size, in bytes, of the image execution info table specified by
  ImageExeInfoTable. If ImageExeInfoTable is NULL, then 0 is returned.

  @param  ImageExeInfoTable          A pointer to a image execution info table structure.
  
  @retval 0       If ImageExeInfoTable is NULL.
  @retval Others  The size of a image execution info table in bytes.

**/
UINTN
GetImageExeInfoTableSize (
  EFI_IMAGE_EXECUTION_INFO_TABLE        *ImageExeInfoTable
  )
{
  UINTN                     Index;
  EFI_IMAGE_EXECUTION_INFO  *ImageExeInfoItem;
  UINTN                     TotalSize;

  if (ImageExeInfoTable == NULL) {
    return 0;
  }

  ImageExeInfoItem  = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoTable + sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE));
  TotalSize         = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);
  for (Index = 0; Index < ImageExeInfoTable->NumberOfImages; Index++) {
    TotalSize += ImageExeInfoItem->InfoSize;
    ImageExeInfoItem = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoItem + ImageExeInfoItem->InfoSize);
  }

  return TotalSize;
}

/**
  Create an Image Execution Information Table entry and add it to system configuration table.

  @param[in]  Action          Describes the action taken by the firmware regarding this image.
  @param[in]  Name            Input a null-terminated, user-friendly name.
  @param[in]  DevicePath      Input device path pointer.
  @param[in]  Signature       Input signature info in EFI_SIGNATURE_LIST data structure.
  @param[in]  SignatureSize   Size of signature.
  
**/
VOID
AddImageExeInfo (
  IN       EFI_IMAGE_EXECUTION_ACTION       Action, 
  IN       CHAR16                           *Name OPTIONAL, 
  IN CONST EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
  IN       EFI_SIGNATURE_LIST               *Signature OPTIONAL,
  IN       UINTN                            SignatureSize
  )
{
  EFI_STATUS                      Status;
  EFI_IMAGE_EXECUTION_INFO_TABLE  *ImageExeInfoTable;
  EFI_IMAGE_EXECUTION_INFO_TABLE  *NewImageExeInfoTable;
  EFI_IMAGE_EXECUTION_INFO        *ImageExeInfoEntry;
  UINTN                           ImageExeInfoTableSize;
  UINTN                           NewImageExeInfoEntrySize;
  UINTN                           NameStringLen;
  UINTN                           DevicePathSize;

  NewImageExeInfoTable  = NULL;
  ImageExeInfoEntry     = NULL;
  NameStringLen         = sizeof (CHAR16);

  if (DevicePath == NULL) {
    return ;
  }

  if (Name != NULL) {
    NameStringLen = StrSize (Name);
  }

  ImageExeInfoTable = GetEfiConfigurationTable(pST, &gEfiImageSecurityDatabaseGuid);
  if (ImageExeInfoTable != NULL) {
    //
    // The table has been found!
    // We must enlarge the table to accmodate the new exe info entry.
    //
    ImageExeInfoTableSize = GetImageExeInfoTableSize (ImageExeInfoTable);
  } else {
    //
    // Not Found!
    // We should create a new table to append to the configuration table.
    //
    ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);
  }

  DevicePathSize            = DPLength(DevicePath);
  NewImageExeInfoEntrySize  = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;

  Status = pBS->AllocatePool(EfiRuntimeServicesData, ImageExeInfoTableSize + NewImageExeInfoEntrySize, &NewImageExeInfoTable);
  if(EFI_ERROR(Status)) return;

  if (ImageExeInfoTable != NULL) {
    CopyMem (NewImageExeInfoTable, ImageExeInfoTable, ImageExeInfoTableSize);
  } else {
    NewImageExeInfoTable->NumberOfImages = 0;
  }
  NewImageExeInfoTable->NumberOfImages++;
  ImageExeInfoEntry = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) NewImageExeInfoTable + ImageExeInfoTableSize);
  //
  // Update new item's infomation.
  //
  ImageExeInfoEntry->Action   = Action;
  ImageExeInfoEntry->InfoSize = (UINT32) NewImageExeInfoEntrySize;

  if (Name != NULL) {
    CopyMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), Name, NameStringLen);
  } else {
    ZeroMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), NameStringLen);
  }
  CopyMem (
    (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen,
    DevicePath,
    DevicePathSize
    );
  if (Signature != NULL) {
    CopyMem (
      (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen + DevicePathSize,
      Signature,
      SignatureSize
      );
  }
  //
  // Update/replace the image execution table.
  //
  Status = pBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) NewImageExeInfoTable);
  ASSERT_EFI_ERROR (Status);
  //
  // Free Old table data!
  //
  if (ImageExeInfoTable != NULL) {
    pBS->FreePool (ImageExeInfoTable);
  }
}

/**
  Create a Boot Image Certificate table entry and add it to system configuration table.

  @param[in]  Name            Input a null-terminated, user-friendly name.
  @param[in]  DevicePath      Input device path pointer.
  @param[in]  Signature       Input signature info in EFI_SIGNATURE_LIST data structure.
  @param[in]  SignatureSize   Size of signature.
  
**/
VOID
AddBootImageCertInfo (
  IN       CHAR16                           *Name OPTIONAL, 
  IN       UINT32                           SignatureOffs,
  IN       UINT32                           SignatureSize
  )
{
    EFI_STATUS                 Status;
    AMI_VALID_CERT_IN_SIG_DB  *BootImageCertInfoTable;
    UINTN                      BootImageCertInfoTableSize;

    if(SignatureOffs == 0 || SignatureSize == 0)
    return;
    
    BootImageCertInfoTable = GetEfiConfigurationTable(pST, &gBootImageCertTblGuid);
    if (BootImageCertInfoTable == NULL) {
        //
        // Not Found!
        // We should create a new table.
        //
        BootImageCertInfoTableSize = sizeof (AMI_VALID_CERT_IN_SIG_DB);
        Status = pBS->AllocatePool(EfiRuntimeServicesData, BootImageCertInfoTableSize, &BootImageCertInfoTable);
        if(EFI_ERROR(Status)) return;
        //
        // Update/replace the image execution table.
        //
        Status = pBS->InstallConfigurationTable (&gBootImageCertTblGuid, (VOID *) BootImageCertInfoTable);
        ASSERT_EFI_ERROR (Status);
        TRACE((TRACE_ALWAYS,"Install BootImageCert Table ...%r\n", Status));
        if (EFI_ERROR (Status)) return;
    }
    //
    // Update Table's infomation.
    //
    BootImageCertInfoTable->SigOffset = SignatureOffs;
    BootImageCertInfoTable->SigLength = SignatureSize;

TRACE((TRACE_ALWAYS,"BootImage Cert in db\nOffset=%X\nLength=%X\n", BootImageCertInfoTable->SigOffset, BootImageCertInfoTable->SigLength));
}

/**
  Discover if the UEFI image is authorized by user's policy setting.

  @param[in]    Policy            Specify platform's policy setting. 
  @param[in]    Action            Image Validation status. 

  @retval EFI_ACCESS_DENIED       Image is not allowed to run.
  @retval EFI_SECURITY_VIOLATION  Image is deferred.
  @retval EFI_SUCCESS             Image is authorized to run.

**/
EFI_STATUS
ImageAuthorization (
  IN UINT32     Policy,
  IN UINT32     Action
  )
{
    EFI_STATUS    Status;
    EFI_INPUT_KEY Key = {0,0};
    UINTN         Index;
    BOOLEAN       bQuery;

    Status = EFI_ACCESS_DENIED;

    switch (Policy) {

    case QUERY_USER_ON_SECURITY_VIOLATION:
        // Conditions to allow overwrite of Execution Policy:
        // 1. Output Console available
        if((pST->ConIn != NULL && pST->ConOut != NULL))
        {
            pST->ConOut->OutputString(pST->ConOut, mNotifyString1);
            bQuery = TRUE;
            switch(Action) {
                case EFI_IMAGE_EXECUTION_AUTH_SIG_NOT_FOUND: // not found in db
                    pST->ConOut->OutputString(pST->ConOut, mNotifyString3);
                    break;
                case EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND:    // found in dbx
                    pST->ConOut->OutputString(pST->ConOut, mNotifyString4);
                    break;
                case EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED:
                case EFI_IMAGE_EXECUTION_AUTH_UNTESTED:
                    pST->ConOut->OutputString(pST->ConOut, mNotifyString5);
                    break;
                default:
                    bQuery = FALSE;
                    break;
            }
            if(!bQuery) break;
            //Wait for key
            pST->ConOut->OutputString(pST->ConOut, mNotifyString2);
Repeat:
            pBS->WaitForEvent( 1, &pST->ConIn->WaitForKey, &Index );
            Status = pST->ConIn->ReadKeyStroke( pST->ConIn, &Key );
            if (!EFI_ERROR (Status)) {
              if ((Key.UnicodeChar == L'y') || (Key.UnicodeChar == L'Y')) { //yes
                Status = EFI_SUCCESS;
              } else if ((Key.UnicodeChar == L'n') || (Key.UnicodeChar == L'N')) { //no
                Status = EFI_ACCESS_DENIED;
              } else {
                goto Repeat;
              }
            }
            pST->ConOut->OutputString(pST->ConOut, L"\r\n");
        } 
        break;

    case DEFER_EXECUTE_ON_SECURITY_VIOLATION:
        Status = EFI_SECURITY_VIOLATION;
        break;

    //  case DENY_EXECUTE_ON_SECURITY_VIOLATION:
    default:
        Status = EFI_ACCESS_DENIED;
        break;
    }

TRACE((TRACE_ALWAYS,"Image Authorization policy...%r\n", Status));

    return Status;
}

/**
  Advanced check in Signature Database
  Check whether signature is located in target database

  @param[in]  Signature           Pointer to signature that is searched for.
  @param[in]  SignatureSize       Size of Signature.
  @param[in]  SignatureType       Type of the Certificate, Guid

  @return TRUE - found in IsSigForbidden            
  @return FALSE            

**/
BOOLEAN
IsSignatureFoundInDatabase (
  IN CHAR16          *Name,
  EFI_GUID           *SignatureType,
  IN UINT8           *Signature, 
  IN UINTN            SignatureSize
  )
{
  EFI_SIGNATURE_LIST  *CertList;
  EFI_SIGNATURE_DATA  *Cert;
  UINT8               *Data;
  UINTN               DataSize;
  UINTN               Index;
  UINTN               CertCount;
  BOOLEAN             IsSigFound;

  //
  // Read signature database variable.
  //
  IsSigFound   = FALSE;

  //
  // Get Signature Database variable.
  //

  Data = GetEfiImageSecurityDatabaseVariableEx (Name, &DataSize);

  if (Data != NULL) {
// not an error if no "dbx" defined
      //
      // Enumerate all signature data in SigDB to check if executable's signature exists.
      //
      CertList = (EFI_SIGNATURE_LIST *) Data;
      while (!IsSigFound && (DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {
        if (!guidcmp(&CertList->SignatureType, SignatureType)) { // Cert types do match 
            CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
            Cert      = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
            if (CertList->SignatureSize == (sizeof(EFI_SIGNATURE_DATA) - 1 + SignatureSize)) {
              for (Index = 0; Index < CertCount; Index++) {
                if (MemCmp(Cert->SignatureData, Signature, SignatureSize) == 0) {
                  //
                  // Find the signature in database.
                  //
                  IsSigFound = TRUE;
                  break;
                }
                Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);
              }
            }
        } // next CertList
        DataSize -= CertList->SignatureListSize;
        CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);
      }
      pBS->FreePool (Data);
  }

  return IsSigFound;
}


/**
  Verify PKCS#7 SignedData using certificate found in Variable which formatted
  as EFI_SIGNATURE_LIST. The Variable may be PK, KEK, DB or DBX.

  @param VariableName  Name of Variable to search for Certificate.

  @retval TRUE         Image pass verification.
  @retval FALSE        Image fail verification.

**/
BOOLEAN
IsPkcsSignedDataVerifiedBySignatureList (
  IN UINT8          *AuthData,
  IN UINTN           AuthDataSize,
  IN CHAR16         *VariableName,
  IN UINT8           Operation
  )
{
    EFI_STATUS               Status;
    EFI_SIGNATURE_LIST      *CertList;
    EFI_SIGNATURE_DATA      *Cert;
    UINT32                   CertCount;
    UINT32                   Index;
    UINT8                   *Data;
    UINTN                    DataSize;
    UINT8                   *RootCert;
    UINTN                    RootCertSize;
    UINT8                   *SigCert;
    UINTN                    SigCertSize;
    BOOLEAN                  IsVerified;
    EFI_TIME                *TimeStamp;
    INT32                    TimeOfRevocationLong;
    UINT8                    ValidCertType;
    BOOLEAN                  bVerifyTimeStampStatus;
    BOOLEAN                  bProcessVerifyTimeStampStatus;

    CertList              = NULL;
    Cert                  = NULL;
    RootCert              = NULL;
    RootCertSize          = 0;
    IsVerified            = FALSE;
    ValidCertType         = CertUndefined;
    bVerifyTimeStampStatus= FALSE;
    bProcessVerifyTimeStampStatus = FALSE;

    mTimeOfSigningLong    = 0;
    TimeOfRevocationLong  = 0;
    // Verify the certificate's header
    //
    // Find certificate in store and verify authenticode struct.
    //
    Data = GetEfiImageSecurityDatabaseVariableEx (VariableName, &DataSize);

    if (!Data) 
        return IsVerified;

TRACE((TRACE_ALWAYS,"\nLook for a match in '%S'===>\n\n", VariableName));

    CertList = (EFI_SIGNATURE_LIST *)Data; 

    //
    // TO DO Optimization. 
    //  1. Find Root CA Cert Ptr or Signer Cert if Root is not found
    //  2. loop thru Trust Cert list and compare each one against Root CA cert(verify SignCert signature with Trust Cert)
    //  3. If found, break the 'while' loop and Pkcs7Verify with Trust Cert
    //  
    //
    // Find Cert Enrolled in database to verify the signature in pkcs7 signed data.
    // 
    // here should be a loop thru Cert list till CertCount
    while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {

        if(!guidcmp(&CertList->SignatureType, &gEfiCertX509Guid))
            ValidCertType = CertX509;
        else
            if(!guidcmp(&CertList->SignatureType, mCertType))
                ValidCertType = CertSha256;
            else
                if(!guidcmp(&CertList->SignatureType, &gEfiCertX509Sha256Guid))
                    ValidCertType = CertX509Sha256;
                else
                    if(!guidcmp(&CertList->SignatureType, &gEfiCertX509Sha384Guid))
                        ValidCertType = CertX509Sha384;
                    else
                        if(!guidcmp(&CertList->SignatureType, &gEfiCertX509Sha512Guid))
                            ValidCertType = CertX509Sha512;

          if (ValidCertType)
          {
            Cert       = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
            CertCount  = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
            RootCertSize  = CertList->SignatureSize-sizeof(EFI_GUID); // sig data structure starts with SigOwner Guid
TRACE((TRACE_ALWAYS,"Signature Type %g\nRootCertSize = %X\n",CertList->SignatureType, RootCertSize));

            for (Index = 0; Index < CertCount; Index++) {
              //
              // Iterate each Signature Data Node within this CertList for verify.
              //
                RootCert = Cert->SignatureData;
                switch(ValidCertType) {
// is Sha256/Sha1 
                case CertSha256:
                    if(RootCertSize == mImageDigestSize && 
                        MemCmp(RootCert, mImageDigest, mImageDigestSize) == 0) {
TRACE((TRACE_ALWAYS,"Hash Cert match found...\n"));
                      //
                      // Found the signature in database.
                      //
                        IsVerified = TRUE;
                    }
                    break;
// x509, x509_ShaXXX
/*
Revocation is true:
- return success && Time = 0
- return success && Time != 0 && VerifyTimeStampStatus
- success 
if success but mVerifyTimeStampStatus = ACCESS_DENIED = return FAIL
timestamp non-0 Year > 0
*/
                case CertX509Sha256:
                case CertX509Sha384:
                case CertX509Sha512:
                    switch(ValidCertType) {
                        case CertX509Sha256:
                            TimeStamp = &((EFI_CERT_X509_SHA256*)RootCert)->TimeOfRevocation;
                            break;
                        case CertX509Sha384:
                            TimeStamp = &((EFI_CERT_X509_SHA384*)RootCert)->TimeOfRevocation;
                            break;
                        case CertX509Sha512:
                            TimeStamp = &((EFI_CERT_X509_SHA512*)RootCert)->TimeOfRevocation;
                            break;
                    }
                    // Check the timestamp cert validity the valid certificate in allowed database (dbt).
                    if(!bProcessVerifyTimeStampStatus) {
                        bVerifyTimeStampStatus = IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE2, Pkcs7TimeStampCertValidateGet); 
                        bProcessVerifyTimeStampStatus = TRUE;
                    }
                    if(os_mktime(TimeStamp->Year, TimeStamp->Month, TimeStamp->Day, TimeStamp->Hour, TimeStamp->Minute, TimeStamp->Second,&TimeOfRevocationLong))
                        TimeOfRevocationLong = 0;
TRACE((TRACE_ALWAYS,"\nTimeStamp cert validate %s\nTimeOfSigning %X\nTimeOfRevocation %X\n",  (!bVerifyTimeStampStatus?"FAIL":"PASS"), mTimeOfSigningLong, TimeOfRevocationLong));
                    if(TimeOfRevocationLong) {
                        if(!bVerifyTimeStampStatus) {
                            IsVerified = TRUE;  // found in dbx -> image is revoked!
                            break;
                        }
                        // keep looking for cert with sign time passed revocation
                       if(mTimeOfSigningLong < TimeOfRevocationLong)
                            break;
                    }
                    case CertX509:
TRACE((TRACE_ALWAYS,"proceed with Pkcs7Verify...\n"));  
                    //
                    //
                    // Verify Authenticode struct for image's current certificate.
                    //
                    // using protocol
                    SigCert       = mImageDigest;
                    SigCertSize   = mImageDigestSize;
                    Status = mDigitalSigProtocol->Pkcs7Verify (
                         mDigitalSigProtocol,
                         AuthData,
                         AuthDataSize,
                         RootCert,
                         RootCertSize,
                        // mDigest, DigestLen
                         &SigCert,
                         &SigCertSize,
                         Operation,             // Operation; Pkcs7CertValidate OR Pkcs7CertGetMatchInCertChain
                         KEEP                   // Flags 
                         );
TRACE((TRACE_ALWAYS,"====> %r\n", Status));
                    if (!EFI_ERROR(Status)) {
                        IsVerified = TRUE; // found matching certificate in the image 
                        switch(Operation) {
                            case Pkcs7TimeStampCertValidateGet:
                                mTimeOfSigningLong = (INT32)SigCertSize;
                                break;    
                            case Pkcs7CertValidate:
                                // only x509 certs frm db are measured in PCR[7]
                                mTrustSigDbOffs = (UINT32)(UINT8*)(((UINT8*)Cert-Data));
                                mTrustSigDbSize = CertList->SignatureSize;
                                break;    
                        }
                    }
                    break;
                   default :
                       goto Exit;
                } // switch
                if (IsVerified)
                {
                    goto Exit;
                }
                Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);
            }  // end for    
        } // end If 

        DataSize -= CertList->SignatureListSize;
        CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);
    } // end while
Exit:
    pBS->FreePool (Data);
TRACE((TRACE_ALWAYS,"\n<===%s cert match in '%S'\n",(IsVerified?"Got":"No"), VariableName));
    return IsVerified;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   DxeImageVerificationHandler
//
// Description: Handle for Security Architectural Protocol
//        Provide verification service for signed images, which include both signature validation
//        and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and
//        MSFT Authenticode type signatures are supported.
//
//        In this implementation, only verify external executables when in USER MODE.
//        Executables from FV is bypass, so pass in AuthenticationStatus is ignored.
//
//        The image verification policy is:
//          If the image is signed,
//            At least one valid signature or at least one hash value of the image must match a record
//            in the security database "db", and no valid signature nor any hash value of the image may
//            be reflected in the security database "dbx".
//          Otherwise, the image is not signed,
//            The SHA256 hash value of the image must match a record in the security database "db", and
//            not be reflected in the security data base "dbx".
//
//        Caution: This function may receive untrusted input.
//          PE/COFF image is external input, so this function will validate its data structure
//          within this image buffer before use.
//
//  Input:
//     File                   This is a pointer to the device path of the file that is
//                            being dispatched. This will optionally be used for logging.
//     FileBuffer             File buffer matches the input file device path.
//     FileSize               Size of File buffer matches the input file device path.
//
// Output:      EFI_STATUS
//
//      EFI_SUCCESS            The file specified by File did authenticate, and the
//                             platform policy dictates that the DXE Core may use File.
//      EFI_INVALID_PARAMETER  File is NULL.
//      EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
//                             the platform policy dictates that File should be placed
//                             in the untrusted state. A file may be promoted from
//                             the untrusted to the trusted state at a future time
//                             with a call to the Trust() DXE Service.
//      EFI_ACCESS_DENIED      The file specified by File did not authenticate, and
//                             the platform policy dictates that File should not be
//                             used for any purpose.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
DxeImageVerificationHandler (
  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
  IN VOID                      *FileBuffer,
  IN UINTN                     FileSize,
  IN  BOOLEAN                  BootPolicy
  )

{
  EFI_STATUS                  Status;
  UINT16                      Magic;
  EFI_IMAGE_DOS_HEADER        *DosHdr;
  EFI_STATUS                  VerifyStatus;
  EFI_SIGNATURE_LIST          *SignatureList;
  UINTN                       SignatureListSize;
  EFI_SIGNATURE_DATA          *Signature;
  EFI_IMAGE_EXECUTION_ACTION  Action;
  WIN_CERTIFICATE             *WinCertificate;
  UINT32                      Policy;
  BOOLEAN                     bFileRead;  

  EFI_PEI_PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  UINT32                               NumberOfRvaAndSizes;

  WIN_CERTIFICATE_EFI_PKCS             *PkcsCertData;
  WIN_CERTIFICATE_UEFI_GUID            *WinCertUefiGuid;
  UINT8                                *AuthData;
  UINTN                                 AuthDataSize;
  EFI_IMAGE_DATA_DIRECTORY            *SecDataDir;
  UINT32                                OffSet;


  SignatureList     = NULL;
  SignatureListSize = 0;
  WinCertificate    = NULL;
  SecDataDir        = NULL;
  PkcsCertData      = NULL;
  Action            = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;
  VerifyStatus      = EFI_ACCESS_DENIED;
  Status            = EFI_ACCESS_DENIED;
  bFileRead         = FALSE;

  mImageDigestSize  = 0;
  mCertType         = NULL;
  //
  // Check the image type and get policy setting.
  //
  switch (GetImageType (File)) {

  case IMAGE_FROM_FV:
    Policy = ALWAYS_EXECUTE;
    break;

  case IMAGE_FROM_OPTION_ROM:
    Policy = mSecureBootSetup.Load_from_OROM;
    break;

  case IMAGE_FROM_REMOVABLE_MEDIA:
    Policy = mSecureBootSetup.Load_from_REMOVABLE_MEDIA;
    break;

  case IMAGE_FROM_FIXED_MEDIA:
    Policy = mSecureBootSetup.Load_from_FIXED_MEDIA;
    break;

  default:
TRACE((TRACE_ALWAYS,"Unknown Image Path\n"));
    Policy = DENY_EXECUTE_ON_SECURITY_VIOLATION;
    break;
  }

  //
  // If policy is always/never execute, return directly.
  //
  if (Policy == ALWAYS_EXECUTE) {
    return EFI_SUCCESS;
  } else 
    if (Policy == NEVER_EXECUTE) {
        return EFI_ACCESS_DENIED;
    }

  if (FileBuffer == NULL) {
  //
  // Read the PE/COFF file.
  //
    TRACE((TRACE_ALWAYS,"FileAuthentication\nFileType = %x, SubType = %x\n", File->Type, File->SubType));    
    Status = ReadImage(FALSE, File, &FileBuffer, &FileSize, NULL, NULL, NULL);
    TRACE((TRACE_ALWAYS,"File read(1)...%r\n", Status));
    if(EFI_ERROR(Status)) {
        Status = ReadImage(TRUE, File, &FileBuffer, &FileSize, NULL, NULL, NULL);
        TRACE((TRACE_ALWAYS,"File read(2)...%r\n", Status));
        if(EFI_ERROR(Status))
            return EFI_ACCESS_DENIED;
    }

    bFileRead = TRUE;
  }
  //
  // Read the Dos header.
  //
  ASSERT (FileBuffer != NULL);
  if (FileBuffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  mImageBase  = (UINT8 *) FileBuffer;
  mImageSize  = FileSize;

  ZeroMem (&ImageContext, sizeof (ImageContext));
  ImageContext.Handle    = (VOID *) FileBuffer;
  ImageContext.ImageRead = (EFI_PEI_PE_COFF_LOADER_READ_FILE) DxeImageVerificationLibImageRead;

  //
  // Get information about the image being loaded
  //
  if (EFI_ERROR (PeCoffLoaderGetImageInfo (NULL, &ImageContext)) ||
      ( mImageSize< sizeof(EFI_IMAGE_DOS_HEADER)) ) {
    //
    // The information can't be gotten from the invalid PeImage
    //
    TRACE((TRACE_ALWAYS,"PeCoffLoaderGetImageInfo Error\n"));
    goto Done;
  }

  DosHdr = (EFI_IMAGE_DOS_HEADER *) mImageBase;
  if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
    //
    // DOS image header is present,
    // so read the PE header after the DOS image header.
    //
    mPeCoffHeaderOffset = DosHdr->e_lfanew;
  } else {
    mPeCoffHeaderOffset = 0;
  }
  // Security review EIP[104046]: 3.HashPeImage does not validate minimum image size
  // Validate mPeCoffHeaderOffset is within the Image size range
  if(((UINT64)mPeCoffHeaderOffset+sizeof(EFI_IMAGE_NT_HEADERS32)) > mImageSize)  {
    TRACE((TRACE_ALWAYS,"EFI_IMAGE_NT_SIGNATURE Error\n"));
    goto Done;
  }

  //
  // Check PE/COFF image.
  //
  mNtHeader.Pe32 = (EFI_IMAGE_NT_HEADERS32 *) (mImageBase + mPeCoffHeaderOffset);
  if (mNtHeader.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
    //
    // It is not a valid Pe/Coff file.
    //
    TRACE((TRACE_ALWAYS,"EFI_IMAGE_NT_SIGNATURE Error\n"));
    goto Done;
  }

  if (mNtHeader.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    //
    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value 
    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the 
    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
    //
    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
  } else {
    //
    // Get the magic value from the PE/COFF Optional Header
    //
    Magic = mNtHeader.Pe32->OptionalHeader.Magic;
  }
  
  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    //
    // Use PE32 offset.
    //
    NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;
    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
    }        
  } else {
    //
    // Use PE32+ offset.
    //
    NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
    }
  }

  TRACE((TRACE_ALWAYS,"mImageSize %x\n", mImageSize));

  //
  // Start Image Validation.
  //
  if (SecDataDir && SecDataDir->Size) {
  //  
  // Image is possibly signed
  //
    TRACE((TRACE_ALWAYS,"SecDataDir->VirtualAddress %x\nSecDataDir->Size = %x\nIMAGE is Signed\n",
          SecDataDir->VirtualAddress, SecDataDir->Size));
  // Security review EIP[104046]: 2.Multiple integer underflows calculating size of certificate data in PE file
    if(((UINT64)SecDataDir->VirtualAddress+SecDataDir->Size) > mImageSize)
        goto Done;

  //
  // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7 
  // "Attribute Certificate Table".
  // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.
  //
    Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;
    for (OffSet = SecDataDir->VirtualAddress;
         OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);
//         OffSet += WinCertificate->dwLength, OffSet += ALIGN_SIZE (OffSet)) {
         OffSet +=(WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {
    
        WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);
        TRACE((TRACE_ALWAYS,"\nOffSet = %x, WinCertificate->dwLength %x\n\n", OffSet, WinCertificate->dwLength));
        if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||
            (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {
          break;
        }
        //
        // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.
        //
        //
        // Verify signature of executables.
        //

        if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
          //
          // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the 
          // Authenticode specification.
          //
          PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;
          if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {
            break;
          }
          AuthData   = PkcsCertData->CertData;
          AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);

        } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
          //
          // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.
          //
          WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;
          if (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {
            break;
          }
          if (guidcmp (&WinCertUefiGuid->CertType, &gEfiCertTypePkcs7Guid)) {
            continue;
          }
          AuthData = WinCertUefiGuid->CertData;
          AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);

        } else {
          if (WinCertificate->dwLength < sizeof (WIN_CERTIFICATE)) {
            break;
          }
          continue;
        }
        //
        // Verify Pkcs signed data type.
        //
        // At least 1 Cert from Pe Image should be in DB
        // None of Certs should be found in DBX 
        //
        // get Digest Algorithm, set mCertType
        // lock mutex, preserve Pkcs7 context
        Status = HashPeImageByType (AuthData, AuthDataSize);
        if (EFI_ERROR (Status)) {
            continue;
        }

        //
        // Check the digital signature against the revoked certificate in forbidden database (dbx).
        //
        if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, Pkcs7CertGetMatchInCertChain)) {

            Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;
            VerifyStatus = EFI_ACCESS_DENIED;
            break;
        } 
        //
        // Check the digital signature against the valid certificate in allowed database (db).
        //
        if (EFI_ERROR (VerifyStatus)) {
            if(IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, Pkcs7CertValidate)) {

                VerifyStatus = EFI_SUCCESS;
            }
        }
        // clear context
        Status = mDigitalSigProtocol->Pkcs7Verify (
             mDigitalSigProtocol,
             AuthData, AuthDataSize,
             NULL, 0,
             NULL, NULL,
             Pkcs7Arg0,             // Dummy op
             RESET                  // Flags 
             );

    } // end multi-sig for
        
//    if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size + ALIGN_SIZE(SecDataDir->Size))) {
    if(Action != EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {
        if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {
            TRACE((TRACE_ALWAYS,"Offset outside of CertDir boundary %X != %x\n", OffSet, SecDataDir->VirtualAddress + SecDataDir->Size));
            //
            // The Size in Certificate Table or the attribute certificate table is corrupted.
            //
            VerifyStatus = EFI_ACCESS_DENIED;
        } else 
            Action = EFI_IMAGE_EXECUTION_AUTH_SIG_NOT_FOUND;
    }

  } // end Signed cert branch

  // Image not Signed or 
  // Image is Signed, its Cert verified in db but not found in DBX
  if(mCertType != &gEfiCertSha256Guid && 
        Action != EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {

    //
    // This image is not signed or signed but found only in db or failed previous checks. 
    // Expected:
    // The SHA256 hash value of the image must match a record in the security database "db", 
    // and not be reflected in the security data base "dbx".
    //
    TRACE((TRACE_ALWAYS,"IMAGE not Signed\n\tor signed with non-Sha256, Try Sha256 Hash Cert...\n"));

    if(!HashPeImage (HASHALG_SHA256))   // init Hash type and calculate PE hash if not done already
      goto Done;

    if(IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, &gEfiCertSha256Guid, mImageDigest, mImageDigestSize)) {
        //
        // Image Hash in forbidden database (DBX)
        //
        VerifyStatus = EFI_ACCESS_DENIED;
        Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;
    } else
        // no - try in allowed db if not there already
        if(EFI_ERROR (VerifyStatus) && 
            IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, &gEfiCertSha256Guid, mImageDigest, mImageDigestSize)) {
            //
            // Image Hash is in allowed database (DB).
            //
            VerifyStatus = EFI_SUCCESS;
        }
  } 
  //
  // Signature database check after verification.
  //
  if(!EFI_ERROR (VerifyStatus) ) {
    //
    // Executable signature is found in authorized signature database.
    //
      Status = EFI_SUCCESS;
  } else {
      Status = EFI_ACCESS_DENIED;

      if(Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {
        //
        // Executable signature is found in forbidden signature database.
        //
        TRACE((TRACE_ALWAYS,"Certificate IS FOUND in Forbidden database!\n"));
      } else 
        //
        // Verification failure.
        //
        if(Action == EFI_IMAGE_EXECUTION_AUTH_SIG_NOT_FOUND) {
        //
        // Executable signature cannot be found in authorized signature database.
        // Use platform policy to determine the action.
        //
            TRACE((TRACE_ALWAYS,"Certificate NOT FOUND in authorized database!\n"));
        } 
        else 
            TRACE((TRACE_ALWAYS,"Image Certificate is Invalid!\n"));

        TRACE((TRACE_ALWAYS,"%r Record added to Image Execution Table\n", Status));

        if(!mCertType || !mImageDigestSize ) {
            goto Done;
        }
        SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;
        
        // Security review EIP[104046]:7. Unchecked AllocatePool may result in NULL pointer dereference
        if (!EFI_ERROR(pBS->AllocatePool(EfiBootServicesData, SignatureListSize, &SignatureList))) 
        {  
          MemSet(SignatureList, SignatureListSize, 0);
          SignatureList->SignatureHeaderSize  = 0;
          SignatureList->SignatureListSize    = (UINT32) SignatureListSize;
          SignatureList->SignatureSize        = (UINT32) mImageDigestSize;
          CopyMem (&SignatureList->SignatureType, mCertType, sizeof (EFI_GUID));
          Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));
          CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);
        }
        if (SignatureList != NULL) {
            pBS->FreePool (SignatureList);
        }
    }
Done:
  if (Status != EFI_SUCCESS) {
    //
    // Policy decides to defer or reject the image; add its information in image executable information table.
    //
    AddImageExeInfo (Action, NULL, File, SignatureList, SignatureListSize);
    // Treat unsecured images according to Image Authorization policy
    Status = ImageAuthorization (Policy, Action);
  }
  if(bFileRead)
      pBS->FreePool (FileBuffer);
    
TRACE((TRACE_ALWAYS,"Image Verification ...%r\n", Status));
  return Status;
}

//===================== Security Architectural Protocol ======================
#ifndef EFI_SECURITY2_ARCH_PROTOCOL_GUID
#define EFI_SECURITY2_ARCH_PROTOCOL_GUID  \
  { 0x94ab2f58, 0x1438, 0x4ef1, 0x91, 0x52, 0x18, 0x94, 0x1a, 0x3a, 0xe, 0x68 }

typedef struct _EFI_SECURITY2_ARCH_PROTOCOL EFI_SECURITY2_ARCH_PROTOCOL;

typedef EFI_STATUS (EFIAPI *EFI_SECURITY2_FILE_AUTHENTICATION) (
    IN CONST EFI_SECURITY2_ARCH_PROTOCOL *This,
    IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
    IN VOID *FileBuffer,
    IN UINTN FileSize,
    IN BOOLEAN BootPolicy
);

struct _EFI_SECURITY2_ARCH_PROTOCOL {
    EFI_SECURITY2_FILE_AUTHENTICATION FileAuthentication;
};

static EFI_GUID gEfiSecurity2ArchProtocolGuid = EFI_SECURITY2_ARCH_PROTOCOL_GUID;
#endif

EFI_STATUS SecureFileAuthentication2(
    IN CONST EFI_SECURITY2_ARCH_PROTOCOL *This,
    IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
    IN VOID *FileBuffer,
    IN UINTN FileSize,
    IN BOOLEAN BootPolicy
    )
{
    EFI_STATUS Status;

    TRACE((TRACE_ALWAYS,"...............Call Security2 Arch proto.. File=%x, FileBuffer=%x, FileSize=%d\n",File,FileBuffer,FileSize ));

    //
    // skip verification if platform is NOT in SECURE BOOT MODE 
    //
    if(mSecureBootEnable == 0)
        return  EFI_SUCCESS;

    //
    // Protocol should have been installed by AmiDigitalSig Driver once it's dispatched 
    //
    if( mDigitalSigProtocol == NULL )
        return EFI_ACCESS_DENIED;

    Status = DxeImageVerificationHandler(File, FileBuffer, FileSize, BootPolicy);
    // Install Tbl only on the Boot Loader launch
    if (!EFI_ERROR (Status)/* && BootPolicy*/) {
        AddBootImageCertInfo(NULL, mTrustSigDbOffs, mTrustSigDbSize);
        mTrustSigDbOffs = 0;
        mTrustSigDbSize = 0;
    }
    TRACE((TRACE_ALWAYS,"...............return status %r\n",Status));
    return Status; 
}

EFI_STATUS SecureFileAuthentication(
    IN EFI_SECURITY_ARCH_PROTOCOL *This,
    IN UINT32 AuthenticationStatus,
    IN EFI_DEVICE_PATH_PROTOCOL *File
    )
{
    BOOLEAN BootPolicy;
    VOID    *FileBuffer;
    UINTN   FileSize;

    BootPolicy  = TRUE;
    FileBuffer  = NULL;
    FileSize    = 0;

    TRACE((TRACE_ALWAYS,".........Call Security Arch proto.. File=%x\n",File));

    if (File == NULL) 
        return EFI_INVALID_PARAMETER;

  //Security Protocol w/a: BootPolicy == 1 if File->Type == 4 Media Dev Path
//    if(File->Type == 4) // Media Dev Path
//        BootPolicy=TRUE;

    return SecureFileAuthentication2(NULL, File, FileBuffer, FileSize, BootPolicy);
}

EFI_SECURITY2_ARCH_PROTOCOL mSecurity2 = {SecureFileAuthentication2};
EFI_SECURITY_ARCH_PROTOCOL mSecurity = {SecureFileAuthentication};

//----------------------------------------------------------------------------
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitDigitalProtocolCallback
//
// Description: This function initialize mDigitalSigProtocol ptr
//              
//
//  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  InitDigitalProtocolCallback(IN EFI_EVENT Event, IN VOID *Context)
{
    EFI_STATUS Status;
    UINTN      DataSize;
    UINT8      *pSecureBootEnable=NULL;
    //
    // Look up for image authorization policy in "SetupData" variable
    //
#if (defined(ENABLE_IMAGE_EXEC_POLICY_OVERRIDE) && ENABLE_IMAGE_EXEC_POLICY_OVERRIDE == 1)
    DataSize = sizeof(mSecureBootSetup);
    Status = pRS->GetVariable (AMI_SECURE_BOOT_SETUP_VAR,&gSecureSetupGuid,NULL,&DataSize,&mSecureBootSetup);
    if(EFI_ERROR(Status) ||
       // Standard boot mode policy->apply defaults
       mSecureBootSetup.SecureBootMode == STANDARD_SECURE_BOOT || 
       // Policy check against fixed settings
       mSecureBootSetup.Load_from_OROM < LOAD_FROM_OROM ||
       mSecureBootSetup.Load_from_REMOVABLE_MEDIA < LOAD_FROM_REMOVABLE_MEDIA ||
       mSecureBootSetup.Load_from_FIXED_MEDIA < LOAD_FROM_FIXED_MEDIA
    )
#endif
    { 
        mSecureBootSetup.Load_from_OROM = LOAD_FROM_OROM;
        mSecureBootSetup.Load_from_REMOVABLE_MEDIA = LOAD_FROM_REMOVABLE_MEDIA;
        mSecureBootSetup.Load_from_FIXED_MEDIA = LOAD_FROM_FIXED_MEDIA;
    }
    //
    // SecureBoot variable to be installed along with NVRAM driver
    //
    pSecureBootEnable = GetEfiGlobalVariableEx (EFI_SECURE_BOOT_NAME, &DataSize);
    if(pSecureBootEnable && DataSize==1) 
        mSecureBootEnable = *pSecureBootEnable;
    //
    // will skip verification if platform is NOT in SECURE BOOT MODE 
    //
    if(mSecureBootEnable == 0)
        return EFI_SUCCESS;

    Status = pBS->LocateHandleBuffer (
                       ByProtocol,
     #if (PI_SPECIFICATION_VERSION < 0x00010000)
                       &gEfiFirmwareVolumeProtocolGuid,
     #else
                       &gEfiFirmwareVolume2ProtocolGuid,
     #endif
                       NULL,
                       &mFvHandlesCount,
                       &mFvHandles
                       );
    if(!EFI_ERROR(Status)){
      gImageLoadingAfterBDS = TRUE;
    }

    return  pBS->LocateProtocol( &gAmiDigitalSignatureProtocolGuid, NULL, &mDigitalSigProtocol );
}

extern BOOLEAN const InstallDummySecurityProtocol;
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InstallSecurityArchProtocol
//
// Description: This function installs the EFI_SECURITY_ARCH_PROTOCOL.
//              It is called at DxeCoreInitialize.
//
// Input:
//  EFI_HANDLE          ImageHandle     Image handle.
//  EFI_SYSTEM_TABLE    *SystemTable    Pointer to the EFI system table.
//
// Output:
//  EFI_SUCCESS : Security Architecture protocols are successfully installed.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID InstallSecurityArchProtocol( 
    IN  EFI_HANDLE              ImageHandle,
    IN  EFI_SYSTEM_TABLE        *SystemTable
)
{
    EFI_HANDLE Handle = NULL;
    EFI_EVENT  Event;
    VOID       *p;

    if (InstallDummySecurityProtocol)
        return; 

    // Enable Security verification at beginning of BDS connect controller phase, 
    // We assume all drivers before the event were launched from internal FV
    // !!! This bds event is only available since core 4.6.3.5.
    RegisterProtocolCallback(
        &BdsConnectDriversProtocolGuid,
        InitDigitalProtocolCallback,
        NULL,
        &Event,
        &p
    );
   // Security2 Arch protocol must be installed before Security Arch protocol
    pBS->InstallMultipleProtocolInterfaces(
        &Handle, 
        &gEfiSecurity2ArchProtocolGuid, &mSecurity2, 
        &gEfiSecurityArchProtocolGuid, &mSecurity,
        NULL
    );
}

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