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