]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
Add comment for modules which have external input.
[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 - 2012, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20
21 **/
22
23 #include "InternalCryptLib.h"
24
25 #include <openssl/objects.h>
26 #include <openssl/x509.h>
27 #include <openssl/pkcs7.h>
28
29
30 /**
31 Verifies the validility 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 procudure
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 *OrigAuthData;
70 UINT8 *SpcIndirectDataContent;
71 UINT8 Asn1Byte;
72 UINTN ContentSize;
73
74 //
75 // Check input parameters.
76 //
77 if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {
78 return FALSE;
79 }
80
81 if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {
82 return FALSE;
83 }
84
85 Status = FALSE;
86 Pkcs7 = NULL;
87 OrigAuthData = AuthData;
88
89 //
90 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature
91 //
92 Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);
93 if (Pkcs7 == NULL) {
94 goto _Exit;
95 }
96
97 //
98 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
99 //
100 if (!PKCS7_type_is_signed (Pkcs7)) {
101 goto _Exit;
102 }
103
104 //
105 // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to
106 // some authenticode-specific structure. Use opaque ASN.1 string to retrieve
107 // PKCS#7 ContentInfo here.
108 //
109 SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);
110
111 //
112 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.
113 //
114 Asn1Byte = *(SpcIndirectDataContent + 1);
115
116 if ((Asn1Byte & 0x80) == 0) {
117 //
118 // Short Form of Length Encoding
119 //
120 ContentSize = (UINTN) (Asn1Byte & 0x7F);
121 //
122 // Skip the SEQUENCE Tag;
123 //
124 SpcIndirectDataContent += 2;
125 } else if ((Asn1Byte & 0x82) == 0x82) {
126 //
127 // Long Form of Length Encoding, only support two bytes.
128 //
129 ContentSize = (UINTN) (*(SpcIndirectDataContent + 2));
130 ContentSize = (ContentSize << 8) + (UINTN)(*(SpcIndirectDataContent + 3));
131 //
132 // Skip the SEQUENCE Tag;
133 //
134 SpcIndirectDataContent += 4;
135 } else {
136 goto _Exit;
137 }
138
139 //
140 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent
141 // defined in Authenticode
142 // NOTE: Need to double-check HashLength here!
143 //
144 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {
145 //
146 // Un-matched PE/COFF Hash Value
147 //
148 goto _Exit;
149 }
150
151 //
152 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature
153 //
154 Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);
155
156 _Exit:
157 //
158 // Release Resources
159 //
160 PKCS7_free (Pkcs7);
161
162 return Status;
163 }