]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptAuthenticode.c
CommitLineData
b7d320f8 1/** @file\r
2 Authenticode Portable Executable Signature Verification over OpenSSL.\r
3\r
dc204d5a
JY
4 Caution: This module requires additional review when modified.\r
5 This library will have external input - signature (e.g. PE/COFF Authenticode).\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8\r
9 AuthenticodeVerify() will get PE/COFF Authenticode and will do basic check for\r
10 data structure.\r
11\r
26442d11 12Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.<BR>\r
2009f6b4 13SPDX-License-Identifier: BSD-2-Clause-Patent\r
b7d320f8 14\r
15**/\r
16\r
17#include "InternalCryptLib.h"\r
18\r
19#include <openssl/objects.h>\r
20#include <openssl/x509.h>\r
21#include <openssl/pkcs7.h>\r
22\r
fe5eea5e 23//\r
24// OID ASN.1 Value for SPC_INDIRECT_DATA_OBJID\r
25//\r
8f837243 26GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 mSpcIndirectOidValue[] = {\r
fe5eea5e 27 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04\r
7c342378 28};\r
b7d320f8 29\r
30/**\r
2998af86 31 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows\r
b7d320f8 32 Authenticode Portable Executable Signature Format".\r
33\r
16d2c32c 34 If AuthData is NULL, then return FALSE.\r
35 If ImageHash is NULL, then return FALSE.\r
b7d320f8 36\r
dc204d5a
JY
37 Caution: This function may receive untrusted input.\r
38 PE/COFF Authenticode is external input, so this function will do basic check for\r
39 Authenticode data structure.\r
40\r
b7d320f8 41 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed\r
42 PE/COFF image to be verified.\r
43 @param[in] DataSize Size of the Authenticode Signature in bytes.\r
44 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r
45 is used for certificate chain verification.\r
46 @param[in] CertSize Size of the trusted certificate in bytes.\r
2998af86 47 @param[in] ImageHash Pointer to the original image file hash value. The procedure\r
b7d320f8 48 for calculating the image hash value is described in Authenticode\r
49 specification.\r
50 @param[in] HashSize Size of Image hash value in bytes.\r
51\r
52 @retval TRUE The specified Authenticode Signature is valid.\r
53 @retval FALSE Invalid Authenticode Signature.\r
54\r
55**/\r
56BOOLEAN\r
57EFIAPI\r
58AuthenticodeVerify (\r
59 IN CONST UINT8 *AuthData,\r
60 IN UINTN DataSize,\r
61 IN CONST UINT8 *TrustedCert,\r
62 IN UINTN CertSize,\r
63 IN CONST UINT8 *ImageHash,\r
64 IN UINTN HashSize\r
65 )\r
66{\r
67 BOOLEAN Status;\r
68 PKCS7 *Pkcs7;\r
1463ce18 69 CONST UINT8 *Temp;\r
b7d320f8 70 CONST UINT8 *OrigAuthData;\r
71 UINT8 *SpcIndirectDataContent;\r
72 UINT8 Asn1Byte;\r
73 UINTN ContentSize;\r
e6eaada4 74 CONST UINT8 *SpcIndirectDataOid;\r
b7d320f8 75\r
76 //\r
16d2c32c 77 // Check input parameters.\r
b7d320f8 78 //\r
16d2c32c 79 if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {\r
80 return FALSE;\r
81 }\r
82\r
83 if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {\r
84 return FALSE;\r
85 }\r
da9e7418 86\r
b7d320f8 87 Status = FALSE;\r
88 Pkcs7 = NULL;\r
89 OrigAuthData = AuthData;\r
90\r
91 //\r
92 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature\r
93 //\r
1463ce18
QL
94 Temp = AuthData;\r
95 Pkcs7 = d2i_PKCS7 (NULL, &Temp, (int)DataSize);\r
b7d320f8 96 if (Pkcs7 == NULL) {\r
97 goto _Exit;\r
98 }\r
99\r
100 //\r
101 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r
102 //\r
26442d11 103 if (!PKCS7_type_is_signed (Pkcs7) || PKCS7_get_detached (Pkcs7)) {\r
b7d320f8 104 goto _Exit;\r
105 }\r
106\r
107 //\r
108 // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to\r
109 // some authenticode-specific structure. Use opaque ASN.1 string to retrieve\r
110 // PKCS#7 ContentInfo here.\r
111 //\r
7c342378
MK
112 SpcIndirectDataOid = OBJ_get0_data (Pkcs7->d.sign->contents->type);\r
113 if ((OBJ_length (Pkcs7->d.sign->contents->type) != sizeof (mSpcIndirectOidValue)) ||\r
114 (CompareMem (\r
115 SpcIndirectDataOid,\r
116 mSpcIndirectOidValue,\r
117 sizeof (mSpcIndirectOidValue)\r
118 ) != 0))\r
119 {\r
fe5eea5e 120 //\r
121 // Un-matched SPC_INDIRECT_DATA_OBJID.\r
122 //\r
123 goto _Exit;\r
2ac68e8b 124 }\r
fe5eea5e 125\r
b7d320f8 126 SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);\r
127\r
128 //\r
129 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.\r
130 //\r
131 Asn1Byte = *(SpcIndirectDataContent + 1);\r
16d2c32c 132\r
b7d320f8 133 if ((Asn1Byte & 0x80) == 0) {\r
134 //\r
2ac68e8b 135 // Short Form of Length Encoding (Length < 128)\r
b7d320f8 136 //\r
7c342378 137 ContentSize = (UINTN)(Asn1Byte & 0x7F);\r
b7d320f8 138 //\r
139 // Skip the SEQUENCE Tag;\r
140 //\r
141 SpcIndirectDataContent += 2;\r
2ac68e8b
QL
142 } else if ((Asn1Byte & 0x81) == 0x81) {\r
143 //\r
144 // Long Form of Length Encoding (128 <= Length < 255, Single Octet)\r
145 //\r
7c342378 146 ContentSize = (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 2));\r
2ac68e8b
QL
147 //\r
148 // Skip the SEQUENCE Tag;\r
149 //\r
150 SpcIndirectDataContent += 3;\r
16d2c32c 151 } else if ((Asn1Byte & 0x82) == 0x82) {\r
b7d320f8 152 //\r
2ac68e8b 153 // Long Form of Length Encoding (Length > 255, Two Octet)\r
b7d320f8 154 //\r
7c342378 155 ContentSize = (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 2));\r
0e241454 156 ContentSize = (ContentSize << 8) + (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 3));\r
b7d320f8 157 //\r
158 // Skip the SEQUENCE Tag;\r
159 //\r
160 SpcIndirectDataContent += 4;\r
16d2c32c 161 } else {\r
162 goto _Exit;\r
b7d320f8 163 }\r
164\r
165 //\r
166 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r
167 // defined in Authenticode\r
168 // NOTE: Need to double-check HashLength here!\r
169 //\r
170 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r
171 //\r
172 // Un-matched PE/COFF Hash Value\r
173 //\r
174 goto _Exit;\r
175 }\r
176\r
177 //\r
178 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r
179 //\r
7c342378 180 Status = (BOOLEAN)Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r
b7d320f8 181\r
182_Exit:\r
183 //\r
184 // Release Resources\r
185 //\r
186 PKCS7_free (Pkcs7);\r
187\r
188 return Status;\r
189}\r