]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
CryptoPkg/BaseCryptLib: fix NULL dereference (CVE-2019-14584)
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptAuthenticode.c
... / ...
CommitLineData
1/** @file\r
2 Authenticode Portable Executable Signature Verification over OpenSSL.\r
3\r
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
12Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.<BR>\r
13SPDX-License-Identifier: BSD-2-Clause-Patent\r
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
23//\r
24// OID ASN.1 Value for SPC_INDIRECT_DATA_OBJID\r
25//\r
26UINT8 mSpcIndirectOidValue[] = {\r
27 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04\r
28 };\r
29\r
30/**\r
31 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows\r
32 Authenticode Portable Executable Signature Format".\r
33\r
34 If AuthData is NULL, then return FALSE.\r
35 If ImageHash is NULL, then return FALSE.\r
36\r
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
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
47 @param[in] ImageHash Pointer to the original image file hash value. The procedure\r
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
69 CONST UINT8 *Temp;\r
70 CONST UINT8 *OrigAuthData;\r
71 UINT8 *SpcIndirectDataContent;\r
72 UINT8 Asn1Byte;\r
73 UINTN ContentSize;\r
74 CONST UINT8 *SpcIndirectDataOid;\r
75\r
76 //\r
77 // Check input parameters.\r
78 //\r
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
86\r
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
94 Temp = AuthData;\r
95 Pkcs7 = d2i_PKCS7 (NULL, &Temp, (int)DataSize);\r
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
103 if (!PKCS7_type_is_signed (Pkcs7) || PKCS7_get_detached (Pkcs7)) {\r
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
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
120 // Un-matched SPC_INDIRECT_DATA_OBJID.\r
121 //\r
122 goto _Exit;\r
123 }\r
124\r
125\r
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
132\r
133 if ((Asn1Byte & 0x80) == 0) {\r
134 //\r
135 // Short Form of Length Encoding (Length < 128)\r
136 //\r
137 ContentSize = (UINTN) (Asn1Byte & 0x7F);\r
138 //\r
139 // Skip the SEQUENCE Tag;\r
140 //\r
141 SpcIndirectDataContent += 2;\r
142\r
143 } else if ((Asn1Byte & 0x81) == 0x81) {\r
144 //\r
145 // Long Form of Length Encoding (128 <= Length < 255, Single Octet)\r
146 //\r
147 ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));\r
148 //\r
149 // Skip the SEQUENCE Tag;\r
150 //\r
151 SpcIndirectDataContent += 3;\r
152\r
153 } else if ((Asn1Byte & 0x82) == 0x82) {\r
154 //\r
155 // Long Form of Length Encoding (Length > 255, Two Octet)\r
156 //\r
157 ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));\r
158 ContentSize = (ContentSize << 8) + (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 3));\r
159 //\r
160 // Skip the SEQUENCE Tag;\r
161 //\r
162 SpcIndirectDataContent += 4;\r
163\r
164 } else {\r
165 goto _Exit;\r
166 }\r
167\r
168 //\r
169 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r
170 // defined in Authenticode\r
171 // NOTE: Need to double-check HashLength here!\r
172 //\r
173 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r
174 //\r
175 // Un-matched PE/COFF Hash Value\r
176 //\r
177 goto _Exit;\r
178 }\r
179\r
180 //\r
181 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r
182 //\r
183 Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r
184\r
185_Exit:\r
186 //\r
187 // Release Resources\r
188 //\r
189 PKCS7_free (Pkcs7);\r
190\r
191 return Status;\r
192}\r