]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptAuthenticode.c
a1f8c58e746de62a8ba7db54ca3a034236ca0c4a
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptAuthenticode.c
1 /** @file
2 Authenticode Portable Executable Signature Verification over OpenSSL.
3
4 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalCryptLib.h"
16
17 #include <openssl/objects.h>
18 #include <openssl/x509.h>
19 #include <openssl/pkcs7.h>
20
21
22 /**
23 Verifies the validility of a PE/COFF Authenticode Signature as described in "Windows
24 Authenticode Portable Executable Signature Format".
25
26 If AuthData is NULL, then return FALSE.
27 If ImageHash is NULL, then return FALSE.
28
29 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
30 PE/COFF image to be verified.
31 @param[in] DataSize Size of the Authenticode Signature in bytes.
32 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
33 is used for certificate chain verification.
34 @param[in] CertSize Size of the trusted certificate in bytes.
35 @param[in] ImageHash Pointer to the original image file hash value. The procudure
36 for calculating the image hash value is described in Authenticode
37 specification.
38 @param[in] HashSize Size of Image hash value in bytes.
39
40 @retval TRUE The specified Authenticode Signature is valid.
41 @retval FALSE Invalid Authenticode Signature.
42
43 **/
44 BOOLEAN
45 EFIAPI
46 AuthenticodeVerify (
47 IN CONST UINT8 *AuthData,
48 IN UINTN DataSize,
49 IN CONST UINT8 *TrustedCert,
50 IN UINTN CertSize,
51 IN CONST UINT8 *ImageHash,
52 IN UINTN HashSize
53 )
54 {
55 BOOLEAN Status;
56 PKCS7 *Pkcs7;
57 CONST UINT8 *OrigAuthData;
58 UINT8 *SpcIndirectDataContent;
59 UINT8 Asn1Byte;
60 UINTN ContentSize;
61
62 //
63 // Check input parameters.
64 //
65 if ((AuthData == NULL) || (TrustedCert == NULL) || (ImageHash == NULL)) {
66 return FALSE;
67 }
68
69 if ((DataSize > INT_MAX) || (CertSize > INT_MAX) || (HashSize > INT_MAX)) {
70 return FALSE;
71 }
72
73 Status = FALSE;
74 Pkcs7 = NULL;
75 OrigAuthData = AuthData;
76
77 //
78 // Retrieve & Parse PKCS#7 Data (DER encoding) from Authenticode Signature
79 //
80 Pkcs7 = d2i_PKCS7 (NULL, &AuthData, (int)DataSize);
81 if (Pkcs7 == NULL) {
82 goto _Exit;
83 }
84
85 //
86 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
87 //
88 if (!PKCS7_type_is_signed (Pkcs7)) {
89 goto _Exit;
90 }
91
92 //
93 // NOTE: OpenSSL PKCS7 Decoder didn't work for Authenticode-format signed data due to
94 // some authenticode-specific structure. Use opaque ASN.1 string to retrieve
95 // PKCS#7 ContentInfo here.
96 //
97 SpcIndirectDataContent = (UINT8 *)(Pkcs7->d.sign->contents->d.other->value.asn1_string->data);
98
99 //
100 // Retrieve the SEQUENCE data size from ASN.1-encoded SpcIndirectDataContent.
101 //
102 Asn1Byte = *(SpcIndirectDataContent + 1);
103
104 if ((Asn1Byte & 0x80) == 0) {
105 //
106 // Short Form of Length Encoding
107 //
108 ContentSize = (UINTN) (Asn1Byte & 0x7F);
109 //
110 // Skip the SEQUENCE Tag;
111 //
112 SpcIndirectDataContent += 2;
113 } else if ((Asn1Byte & 0x82) == 0x82) {
114 //
115 // Long Form of Length Encoding, only support two bytes.
116 //
117 ContentSize = (UINTN) (*(SpcIndirectDataContent + 2));
118 ContentSize = (ContentSize << 8) + (UINTN)(*(SpcIndirectDataContent + 3));
119 //
120 // Skip the SEQUENCE Tag;
121 //
122 SpcIndirectDataContent += 4;
123 } else {
124 goto _Exit;
125 }
126
127 //
128 // Compare the original file hash value to the digest retrieve from SpcIndirectDataContent
129 // defined in Authenticode
130 // NOTE: Need to double-check HashLength here!
131 //
132 if (CompareMem (SpcIndirectDataContent + ContentSize - HashSize, ImageHash, HashSize) != 0) {
133 //
134 // Un-matched PE/COFF Hash Value
135 //
136 goto _Exit;
137 }
138
139 //
140 // Verifies the PKCS#7 Signed Data in PE/COFF Authenticode Signature
141 //
142 Status = (BOOLEAN) Pkcs7Verify (OrigAuthData, DataSize, TrustedCert, CertSize, SpcIndirectDataContent, ContentSize);
143
144 _Exit:
145 //
146 // Release Resources
147 //
148 PKCS7_free (Pkcs7);
149
150 return Status;
151 }