2 Authenticode Portable Executable Signature Verification over OpenSSL.
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.
9 AuthenticodeVerify() will get PE/COFF Authenticode and will do basic check for
12 Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.<BR>
13 SPDX-License-Identifier: BSD-2-Clause-Patent
17 #include "InternalCryptLib.h"
19 #include <openssl/objects.h>
20 #include <openssl/x509.h>
21 #include <openssl/pkcs7.h>
24 // OID ASN.1 Value for SPC_INDIRECT_DATA_OBJID
26 UINT8 mSpcIndirectOidValue
[] = {
27 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04
31 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
32 Authenticode Portable Executable Signature Format".
34 If AuthData is NULL, then return FALSE.
35 If ImageHash is NULL, then return FALSE.
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.
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
50 @param[in] HashSize Size of Image hash value in bytes.
52 @retval TRUE The specified Authenticode Signature is valid.
53 @retval FALSE Invalid Authenticode Signature.
59 IN CONST UINT8
*AuthData
,
61 IN CONST UINT8
*TrustedCert
,
63 IN CONST UINT8
*ImageHash
,
70 CONST UINT8
*OrigAuthData
;
71 UINT8
*SpcIndirectDataContent
;
74 CONST UINT8
*SpcIndirectDataOid
;
77 // Check input parameters.
79 if ((AuthData
== NULL
) || (TrustedCert
== NULL
) || (ImageHash
== NULL
)) {
83 if ((DataSize
> INT_MAX
) || (CertSize
> INT_MAX
) || (HashSize
> INT_MAX
)) {
89 OrigAuthData
= AuthData
;
92 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature
95 Pkcs7
= d2i_PKCS7 (NULL
, &Temp
, (int)DataSize
);
101 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
103 if (!PKCS7_type_is_signed (Pkcs7
) || PKCS7_get_detached (Pkcs7
)) {
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.
112 SpcIndirectDataOid
= OBJ_get0_data (Pkcs7
->d
.sign
->contents
->type
);
113 if ((OBJ_length (Pkcs7
->d
.sign
->contents
->type
) != sizeof (mSpcIndirectOidValue
)) ||
116 mSpcIndirectOidValue
,
117 sizeof (mSpcIndirectOidValue
)
121 // Un-matched SPC_INDIRECT_DATA_OBJID.
126 SpcIndirectDataContent
= (UINT8
*)(Pkcs7
->d
.sign
->contents
->d
.other
->value
.asn1_string
->data
);
129 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.
131 Asn1Byte
= *(SpcIndirectDataContent
+ 1);
133 if ((Asn1Byte
& 0x80) == 0) {
135 // Short Form of Length Encoding (Length < 128)
137 ContentSize
= (UINTN
)(Asn1Byte
& 0x7F);
139 // Skip the SEQUENCE Tag;
141 SpcIndirectDataContent
+= 2;
142 } else if ((Asn1Byte
& 0x81) == 0x81) {
144 // Long Form of Length Encoding (128 <= Length < 255, Single Octet)
146 ContentSize
= (UINTN
)(*(UINT8
*)(SpcIndirectDataContent
+ 2));
148 // Skip the SEQUENCE Tag;
150 SpcIndirectDataContent
+= 3;
151 } else if ((Asn1Byte
& 0x82) == 0x82) {
153 // Long Form of Length Encoding (Length > 255, Two Octet)
155 ContentSize
= (UINTN
)(*(UINT8
*)(SpcIndirectDataContent
+ 2));
156 ContentSize
= (ContentSize
<< 8) + (UINTN
)(*(UINT8
*)(SpcIndirectDataContent
+ 3));
158 // Skip the SEQUENCE Tag;
160 SpcIndirectDataContent
+= 4;
166 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent
167 // defined in Authenticode
168 // NOTE: Need to double-check HashLength here!
170 if (CompareMem (SpcIndirectDataContent
+ ContentSize
- HashSize
, ImageHash
, HashSize
) != 0) {
172 // Un-matched PE/COFF Hash Value
178 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature
180 Status
= (BOOLEAN
)Pkcs7Verify (OrigAuthData
, DataSize
, TrustedCert
, CertSize
, SpcIndirectDataContent
, ContentSize
);