]>
Commit | Line | Data |
---|---|---|
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 | |
12 | Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
13 | This program and the accompanying materials\r | |
14 | are licensed and made available under the terms and conditions of the BSD License\r | |
15 | which accompanies this distribution. The full text of the license may be found at\r | |
16 | http://opensource.org/licenses/bsd-license.php\r | |
17 | \r | |
18 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
19 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
20 | \r | |
21 | **/\r | |
22 | \r | |
23 | #include "InternalCryptLib.h"\r | |
24 | \r | |
25 | #include <openssl/objects.h>\r | |
26 | #include <openssl/x509.h>\r | |
27 | #include <openssl/pkcs7.h>\r | |
28 | \r | |
29 | //\r | |
30 | // OID ASN.1 Value for SPC_INDIRECT_DATA_OBJID\r | |
31 | //\r | |
32 | UINT8 mSpcIndirectOidValue[] = {\r | |
33 | 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04\r | |
34 | };\r | |
35 | \r | |
36 | /**\r | |
37 | Verifies the validility of a PE/COFF Authenticode Signature as described in "Windows\r | |
38 | Authenticode Portable Executable Signature Format".\r | |
39 | \r | |
40 | If AuthData is NULL, then return FALSE.\r | |
41 | If ImageHash is NULL, then return FALSE.\r | |
42 | \r | |
43 | Caution: This function may receive untrusted input.\r | |
44 | PE/COFF Authenticode is external input, so this function will do basic check for\r | |
45 | Authenticode data structure.\r | |
46 | \r | |
47 | @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed\r | |
48 | PE/COFF image to be verified.\r | |
49 | @param[in] DataSize Size of the Authenticode Signature in bytes.\r | |
50 | @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r | |
51 | is used for certificate chain verification.\r | |
52 | @param[in] CertSize Size of the trusted certificate in bytes.\r | |
53 | @param[in] ImageHash Pointer to the original image file hash value. The procudure\r | |
54 | for calculating the image hash value is described in Authenticode\r | |
55 | specification.\r | |
56 | @param[in] HashSize Size of Image hash value in bytes.\r | |
57 | \r | |
58 | @retval TRUE The specified Authenticode Signature is valid.\r | |
59 | @retval FALSE Invalid Authenticode Signature.\r | |
60 | \r | |
61 | **/\r | |
62 | BOOLEAN\r | |
63 | EFIAPI\r | |
64 | AuthenticodeVerify (\r | |
65 | IN CONST UINT8 *AuthData,\r | |
66 | IN UINTN DataSize,\r | |
67 | IN CONST UINT8 *TrustedCert,\r | |
68 | IN UINTN CertSize,\r | |
69 | IN CONST UINT8 *ImageHash,\r | |
70 | IN UINTN HashSize\r | |
71 | )\r | |
72 | {\r | |
73 | BOOLEAN Status;\r | |
74 | PKCS7 *Pkcs7;\r | |
75 | CONST UINT8 *OrigAuthData;\r | |
76 | UINT8 *SpcIndirectDataContent;\r | |
77 | UINT8 Asn1Byte;\r | |
78 | UINTN ContentSize;\r | |
79 | UINT8 *SpcIndirectDataOid;\r | |
80 | \r | |
81 | //\r | |
82 | // Check input parameters.\r | |
83 | //\r | |
84 | if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {\r | |
85 | return FALSE;\r | |
86 | }\r | |
87 | \r | |
88 | if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {\r | |
89 | return FALSE;\r | |
90 | }\r | |
91 | \r | |
92 | Status = FALSE;\r | |
93 | Pkcs7 = NULL;\r | |
94 | OrigAuthData = AuthData;\r | |
95 | \r | |
96 | //\r | |
97 | // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature\r | |
98 | //\r | |
99 | Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);\r | |
100 | if (Pkcs7 == NULL) {\r | |
101 | goto _Exit;\r | |
102 | }\r | |
103 | \r | |
104 | //\r | |
105 | // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r | |
106 | //\r | |
107 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
108 | goto _Exit;\r | |
109 | }\r | |
110 | \r | |
111 | //\r | |
112 | // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to\r | |
113 | // some authenticode-specific structure. Use opaque ASN.1 string to retrieve\r | |
114 | // PKCS#7 ContentInfo here.\r | |
115 | //\r | |
116 | SpcIndirectDataOid = (UINT8 *)(Pkcs7->d.sign->contents->type->data);\r | |
117 | if (CompareMem (\r | |
118 | SpcIndirectDataOid,\r | |
119 | mSpcIndirectOidValue,\r | |
120 | sizeof (mSpcIndirectOidValue)\r | |
121 | ) != 0) {\r | |
122 | //\r | |
123 | // Un-matched SPC_INDIRECT_DATA_OBJID.\r | |
124 | //\r | |
125 | goto _Exit;\r | |
126 | }\r | |
127 | \r | |
128 | \r | |
129 | SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);\r | |
130 | \r | |
131 | //\r | |
132 | // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.\r | |
133 | //\r | |
134 | Asn1Byte = *(SpcIndirectDataContent + 1);\r | |
135 | \r | |
136 | if ((Asn1Byte & 0x80) == 0) {\r | |
137 | //\r | |
138 | // Short Form of Length Encoding (Length < 128)\r | |
139 | //\r | |
140 | ContentSize = (UINTN) (Asn1Byte & 0x7F);\r | |
141 | //\r | |
142 | // Skip the SEQUENCE Tag;\r | |
143 | //\r | |
144 | SpcIndirectDataContent += 2;\r | |
145 | \r | |
146 | } else if ((Asn1Byte & 0x81) == 0x81) {\r | |
147 | //\r | |
148 | // Long Form of Length Encoding (128 <= Length < 255, Single Octet)\r | |
149 | //\r | |
150 | ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));\r | |
151 | //\r | |
152 | // Skip the SEQUENCE Tag;\r | |
153 | //\r | |
154 | SpcIndirectDataContent += 3;\r | |
155 | \r | |
156 | } else if ((Asn1Byte & 0x82) == 0x82) {\r | |
157 | //\r | |
158 | // Long Form of Length Encoding (Length > 255, Two Octet)\r | |
159 | //\r | |
160 | ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));\r | |
161 | ContentSize = (ContentSize << 8) + (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 3));\r | |
162 | //\r | |
163 | // Skip the SEQUENCE Tag;\r | |
164 | //\r | |
165 | SpcIndirectDataContent += 4;\r | |
166 | \r | |
167 | } else {\r | |
168 | goto _Exit;\r | |
169 | }\r | |
170 | \r | |
171 | //\r | |
172 | // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r | |
173 | // defined in Authenticode\r | |
174 | // NOTE: Need to double-check HashLength here!\r | |
175 | //\r | |
176 | if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r | |
177 | //\r | |
178 | // Un-matched PE/COFF Hash Value\r | |
179 | //\r | |
180 | goto _Exit;\r | |
181 | }\r | |
182 | \r | |
183 | //\r | |
184 | // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r | |
185 | //\r | |
186 | Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r | |
187 | \r | |
188 | _Exit:\r | |
189 | //\r | |
190 | // Release Resources\r | |
191 | //\r | |
192 | PKCS7_free (Pkcs7);\r | |
193 | \r | |
194 | return Status;\r | |
195 | }\r |