]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
1. Fix build break issue for NOOPT target.
[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
4Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT 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
44BOOLEAN\r
45EFIAPI\r
46AuthenticodeVerify (\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
63 // ASSERT if Authenticode Signature Data or PE Image Hash is NULL\r
64 //\r
65 ASSERT (AuthData != NULL);\r
66 ASSERT (ImageHash != NULL);\r
67\r
da9e7418 68 if (DataSize > INT_MAX) {\r
69 return FALSE;\r
70 }\r
71\r
b7d320f8 72 Status = FALSE;\r
73 Pkcs7 = NULL;\r
74 OrigAuthData = AuthData;\r
75\r
76 //\r
77 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature\r
78 //\r
79 Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);\r
80 if (Pkcs7 == NULL) {\r
81 goto _Exit;\r
82 }\r
83\r
84 //\r
85 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r
86 //\r
87 if (!PKCS7_type_is_signed (Pkcs7)) {\r
88 goto _Exit;\r
89 }\r
90\r
91 //\r
92 // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to\r
93 // some authenticode-specific structure. Use opaque ASN.1 string to retrieve\r
94 // PKCS#7 ContentInfo here.\r
95 //\r
96 SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);\r
97\r
98 //\r
99 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.\r
100 //\r
101 Asn1Byte = *(SpcIndirectDataContent + 1);\r
102 if ((Asn1Byte & 0x80) == 0) {\r
103 //\r
104 // Short Form of Length Encoding\r
105 //\r
106 ContentSize = (UINTN) (Asn1Byte & 0x7F);\r
107 //\r
108 // Skip the SEQUENCE Tag;\r
109 //\r
110 SpcIndirectDataContent += 2;\r
111 } else {\r
112 //\r
113 // Long Form of Length Encoding (Assume Only two bytes here)\r
114 //\r
115 ContentSize = (UINTN) (*(SpcIndirectDataContent + 2));\r
116 ContentSize = (ContentSize << 8) + (UINTN)(*(SpcIndirectDataContent + 3));\r
117 //\r
118 // Skip the SEQUENCE Tag;\r
119 //\r
120 SpcIndirectDataContent += 4;\r
121 }\r
122\r
123 //\r
124 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent\r
125 // defined in Authenticode\r
126 // NOTE: Need to double-check HashLength here!\r
127 //\r
128 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {\r
129 //\r
130 // Un-matched PE/COFF Hash Value\r
131 //\r
132 goto _Exit;\r
133 }\r
134\r
135 //\r
136 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature\r
137 //\r
138 Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);\r
139\r
140_Exit:\r
141 //\r
142 // Release Resources\r
143 //\r
144 PKCS7_free (Pkcs7);\r
145\r
146 return Status;\r
147}\r