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