]>
Commit | Line | Data |
---|---|---|
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 | |
16d2c32c | 12 | Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r |
b7d320f8 | 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 | /**\r | |
31 | Verifies the validility of a PE/COFF Authenticode Signature as described in "Windows\r | |
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 | |
47 | @param[in] ImageHash Pointer to the original image file hash value. The procudure\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 | |
56 | BOOLEAN\r | |
57 | EFIAPI\r | |
58 | AuthenticodeVerify (\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 *OrigAuthData;\r | |
70 | UINT8 *SpcIndirectDataContent;\r | |
71 | UINT8 Asn1Byte;\r | |
72 | UINTN ContentSize;\r | |
73 | \r | |
74 | //\r | |
16d2c32c | 75 | // Check input parameters.\r |
b7d320f8 | 76 | //\r |
16d2c32c | 77 | if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {\r |
78 | return FALSE;\r | |
79 | }\r | |
80 | \r | |
81 | if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {\r | |
82 | return FALSE;\r | |
83 | }\r | |
da9e7418 | 84 | \r |
b7d320f8 | 85 | Status = FALSE;\r |
86 | Pkcs7 = NULL;\r | |
87 | OrigAuthData = AuthData;\r | |
88 | \r | |
89 | //\r | |
90 | // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature\r | |
91 | //\r | |
92 | Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);\r | |
93 | if (Pkcs7 == NULL) {\r | |
94 | goto _Exit;\r | |
95 | }\r | |
96 | \r | |
97 | //\r | |
98 | // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r | |
99 | //\r | |
100 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
101 | goto _Exit;\r | |
102 | }\r | |
103 | \r | |
104 | //\r | |
105 | // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to\r | |
106 | // some authenticode-specific structure. Use opaque ASN.1 string to retrieve\r | |
107 | // PKCS#7 ContentInfo here.\r | |
108 | //\r | |
109 | SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);\r | |
110 | \r | |
111 | //\r | |
112 | // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.\r | |
113 | //\r | |
114 | Asn1Byte = *(SpcIndirectDataContent + 1);\r | |
16d2c32c | 115 | \r |
b7d320f8 | 116 | if ((Asn1Byte & 0x80) == 0) {\r |
117 | //\r | |
118 | // Short Form of Length Encoding\r | |
119 | //\r | |
120 | ContentSize = (UINTN) (Asn1Byte & 0x7F);\r | |
121 | //\r | |
122 | // Skip the SEQUENCE Tag;\r | |
123 | //\r | |
124 | SpcIndirectDataContent += 2;\r | |
16d2c32c | 125 | } else if ((Asn1Byte & 0x82) == 0x82) {\r |
b7d320f8 | 126 | //\r |
16d2c32c | 127 | // Long Form of Length Encoding, only support two bytes.\r |
b7d320f8 | 128 | //\r |
129 | ContentSize = (UINTN) (*(SpcIndirectDataContent + 2));\r | |
130 | ContentSize = (ContentSize << 8) + (UINTN)(*(SpcIndirectDataContent + 3));\r | |
131 | //\r | |
132 | // Skip the SEQUENCE Tag;\r | |
133 | //\r | |
134 | SpcIndirectDataContent += 4;\r | |
16d2c32c | 135 | } else {\r |
136 | goto _Exit;\r | |
b7d320f8 | 137 | }\r |
138 | \r | |
139 | //\r | |
140 | // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r | |
141 | // defined in Authenticode\r | |
142 | // NOTE: Need to double-check HashLength here!\r | |
143 | //\r | |
144 | if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r | |
145 | //\r | |
146 | // Un-matched PE/COFF Hash Value\r | |
147 | //\r | |
148 | goto _Exit;\r | |
149 | }\r | |
150 | \r | |
151 | //\r | |
152 | // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r | |
153 | //\r | |
154 | Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r | |
155 | \r | |
156 | _Exit:\r | |
157 | //\r | |
158 | // Release Resources\r | |
159 | //\r | |
160 | PKCS7_free (Pkcs7);\r | |
161 | \r | |
162 | return Status;\r | |
163 | }\r |