]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
3c2d14a88bce2a3ac5cddf147c8b101c16b40eb4
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptAuthenticode.c
1 /** @file
2 Authenticode Portable Executable Signature Verification over OpenSSL.
3
4 Caution: This module requires additional review when modified.
5 This library will have external input - signature (e.g. PE/COFF Authenticode).
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8
9 AuthenticodeVerify() will get PE/COFF Authenticode and will do basic check for
10 data structure.
11
12 Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.<BR>
13 SPDX-License-Identifier: BSD-2-Clause-Patent
14
15 **/
16
17 #include "InternalCryptLib.h"
18
19 #include <openssl/objects.h>
20 #include <openssl/x509.h>
21 #include <openssl/pkcs7.h>
22
23 //
24 // OID ASN.1 Value for SPC_INDIRECT_DATA_OBJID
25 //
26 UINT8 mSpcIndirectOidValue[] = {
27 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04
28 };
29
30 /**
31 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
32 Authenticode Portable Executable Signature Format".
33
34 If AuthData is NULL, then return FALSE.
35 If ImageHash is NULL, then return FALSE.
36
37 Caution: This function may receive untrusted input.
38 PE/COFF Authenticode is external input, so this function will do basic check for
39 Authenticode data structure.
40
41 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
42 PE/COFF image to be verified.
43 @param[in] DataSize Size of the Authenticode Signature in bytes.
44 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
45 is used for certificate chain verification.
46 @param[in] CertSize Size of the trusted certificate in bytes.
47 @param[in] ImageHash Pointer to the original image file hash value. The procedure
48 for calculating the image hash value is described in Authenticode
49 specification.
50 @param[in] HashSize Size of Image hash value in bytes.
51
52 @retval TRUE The specified Authenticode Signature is valid.
53 @retval FALSE Invalid Authenticode Signature.
54
55 **/
56 BOOLEAN
57 EFIAPI
58 AuthenticodeVerify (
59 IN CONST UINT8 *AuthData,
60 IN UINTN DataSize,
61 IN CONST UINT8 *TrustedCert,
62 IN UINTN CertSize,
63 IN CONST UINT8 *ImageHash,
64 IN UINTN HashSize
65 )
66 {
67 BOOLEAN Status;
68 PKCS7 *Pkcs7;
69 CONST UINT8 *Temp;
70 CONST UINT8 *OrigAuthData;
71 UINT8 *SpcIndirectDataContent;
72 UINT8 Asn1Byte;
73 UINTN ContentSize;
74 CONST UINT8 *SpcIndirectDataOid;
75
76 //
77 // Check input parameters.
78 //
79 if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {
80 return FALSE;
81 }
82
83 if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {
84 return FALSE;
85 }
86
87 Status = FALSE;
88 Pkcs7 = NULL;
89 OrigAuthData = AuthData;
90
91 //
92 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature
93 //
94 Temp = AuthData;
95 Pkcs7 = d2i_PKCS7 (NULL, &Temp, (int)DataSize);
96 if (Pkcs7 == NULL) {
97 goto _Exit;
98 }
99
100 //
101 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
102 //
103 if (!PKCS7_type_is_signed (Pkcs7) || PKCS7_get_detached (Pkcs7)) {
104 goto _Exit;
105 }
106
107 //
108 // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to
109 // some authenticode-specific structure. Use opaque ASN.1 string to retrieve
110 // PKCS#7 ContentInfo here.
111 //
112 SpcIndirectDataOid = OBJ_get0_data(Pkcs7->d.sign->contents->type);
113 if (OBJ_length(Pkcs7->d.sign->contents->type) != sizeof(mSpcIndirectOidValue) ||
114 CompareMem (
115 SpcIndirectDataOid,
116 mSpcIndirectOidValue,
117 sizeof (mSpcIndirectOidValue)
118 ) != 0) {
119 //
120 // Un-matched SPC_INDIRECT_DATA_OBJID.
121 //
122 goto _Exit;
123 }
124
125
126 SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);
127
128 //
129 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.
130 //
131 Asn1Byte = *(SpcIndirectDataContent + 1);
132
133 if ((Asn1Byte & 0x80) == 0) {
134 //
135 // Short Form of Length Encoding (Length < 128)
136 //
137 ContentSize = (UINTN) (Asn1Byte & 0x7F);
138 //
139 // Skip the SEQUENCE Tag;
140 //
141 SpcIndirectDataContent += 2;
142
143 } else if ((Asn1Byte & 0x81) == 0x81) {
144 //
145 // Long Form of Length Encoding (128 <= Length < 255, Single Octet)
146 //
147 ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));
148 //
149 // Skip the SEQUENCE Tag;
150 //
151 SpcIndirectDataContent += 3;
152
153 } else if ((Asn1Byte & 0x82) == 0x82) {
154 //
155 // Long Form of Length Encoding (Length > 255, Two Octet)
156 //
157 ContentSize = (UINTN) (*(UINT8 *)(SpcIndirectDataContent + 2));
158 ContentSize = (ContentSize << 8) + (UINTN)(*(UINT8 *)(SpcIndirectDataContent + 3));
159 //
160 // Skip the SEQUENCE Tag;
161 //
162 SpcIndirectDataContent += 4;
163
164 } else {
165 goto _Exit;
166 }
167
168 //
169 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent
170 // defined in Authenticode
171 // NOTE: Need to double-check HashLength here!
172 //
173 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {
174 //
175 // Un-matched PE/COFF Hash Value
176 //
177 goto _Exit;
178 }
179
180 //
181 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature
182 //
183 Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);
184
185 _Exit:
186 //
187 // Release Resources
188 //
189 PKCS7_free (Pkcs7);
190
191 return Status;
192 }