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