]>
Commit | Line | Data |
---|---|---|
b7d320f8 | 1 | /** @file\r |
2 | Authenticode Portable Executable Signature Verification over OpenSSL.\r | |
3 | \r | |
4 | Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "InternalCryptLib.h"\r | |
16 | \r | |
17 | #include <openssl/objects.h>\r | |
18 | #include <openssl/x509.h>\r | |
19 | #include <openssl/pkcs7.h>\r | |
20 | \r | |
21 | \r | |
22 | /**\r | |
23 | Verifies the validility of a PE/COFF Authenticode Signature as described in "Windows\r | |
24 | Authenticode Portable Executable Signature Format".\r | |
25 | \r | |
26 | If AuthData is NULL, then ASSERT().\r | |
27 | If ImageHash is NULL, then ASSERT().\r | |
28 | \r | |
29 | @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed\r | |
30 | PE/COFF image to be verified.\r | |
31 | @param[in] DataSize Size of the Authenticode Signature in bytes.\r | |
32 | @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r | |
33 | is used for certificate chain verification.\r | |
34 | @param[in] CertSize Size of the trusted certificate in bytes.\r | |
35 | @param[in] ImageHash Pointer to the original image file hash value. The procudure\r | |
36 | for calculating the image hash value is described in Authenticode\r | |
37 | specification.\r | |
38 | @param[in] HashSize Size of Image hash value in bytes.\r | |
39 | \r | |
40 | @retval TRUE The specified Authenticode Signature is valid.\r | |
41 | @retval FALSE Invalid Authenticode Signature.\r | |
42 | \r | |
43 | **/\r | |
44 | BOOLEAN\r | |
45 | EFIAPI\r | |
46 | AuthenticodeVerify (\r | |
47 | IN CONST UINT8 *AuthData,\r | |
48 | IN UINTN DataSize,\r | |
49 | IN CONST UINT8 *TrustedCert,\r | |
50 | IN UINTN CertSize,\r | |
51 | IN CONST UINT8 *ImageHash,\r | |
52 | IN UINTN HashSize\r | |
53 | )\r | |
54 | {\r | |
55 | BOOLEAN Status;\r | |
56 | PKCS7 *Pkcs7;\r | |
57 | CONST UINT8 *OrigAuthData;\r | |
58 | UINT8 *SpcIndirectDataContent;\r | |
59 | UINT8 Asn1Byte;\r | |
60 | UINTN ContentSize;\r | |
61 | \r | |
62 | //\r | |
d3945da6 | 63 | // ASSERT if Authenticode Signature Data or PE Image Hash is NULL.\r |
b7d320f8 | 64 | //\r |
65 | ASSERT (AuthData != NULL);\r | |
66 | ASSERT (ImageHash != NULL);\r | |
d3945da6 | 67 | ASSERT (DataSize <= INT_MAX);\r |
da9e7418 | 68 | \r |
b7d320f8 | 69 | Status = FALSE;\r |
70 | Pkcs7 = NULL;\r | |
71 | OrigAuthData = AuthData;\r | |
72 | \r | |
73 | //\r | |
74 | // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature\r | |
75 | //\r | |
76 | Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);\r | |
77 | if (Pkcs7 == NULL) {\r | |
78 | goto _Exit;\r | |
79 | }\r | |
80 | \r | |
81 | //\r | |
82 | // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r | |
83 | //\r | |
84 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
85 | goto _Exit;\r | |
86 | }\r | |
87 | \r | |
88 | //\r | |
89 | // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to\r | |
90 | // some authenticode-specific structure. Use opaque ASN.1 string to retrieve\r | |
91 | // PKCS#7 ContentInfo here.\r | |
92 | //\r | |
93 | SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);\r | |
94 | \r | |
95 | //\r | |
96 | // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.\r | |
97 | //\r | |
98 | Asn1Byte = *(SpcIndirectDataContent + 1);\r | |
99 | if ((Asn1Byte & 0x80) == 0) {\r | |
100 | //\r | |
101 | // Short Form of Length Encoding\r | |
102 | //\r | |
103 | ContentSize = (UINTN) (Asn1Byte & 0x7F);\r | |
104 | //\r | |
105 | // Skip the SEQUENCE Tag;\r | |
106 | //\r | |
107 | SpcIndirectDataContent += 2;\r | |
108 | } else {\r | |
109 | //\r | |
110 | // Long Form of Length Encoding (Assume Only two bytes here)\r | |
111 | //\r | |
112 | ContentSize = (UINTN) (*(SpcIndirectDataContent + 2));\r | |
113 | ContentSize = (ContentSize << 8) + (UINTN)(*(SpcIndirectDataContent + 3));\r | |
114 | //\r | |
115 | // Skip the SEQUENCE Tag;\r | |
116 | //\r | |
117 | SpcIndirectDataContent += 4;\r | |
118 | }\r | |
119 | \r | |
120 | //\r | |
121 | // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r | |
122 | // defined in Authenticode\r | |
123 | // NOTE: Need to double-check HashLength here!\r | |
124 | //\r | |
125 | if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r | |
126 | //\r | |
127 | // Un-matched PE/COFF Hash Value\r | |
128 | //\r | |
129 | goto _Exit;\r | |
130 | }\r | |
131 | \r | |
132 | //\r | |
133 | // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r | |
134 | //\r | |
135 | Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r | |
136 | \r | |
137 | _Exit:\r | |
138 | //\r | |
139 | // Release Resources\r | |
140 | //\r | |
141 | PKCS7_free (Pkcs7);\r | |
142 | \r | |
143 | return Status;\r | |
144 | }\r |