TianoCore EDK2 master
Loading...
Searching...
No Matches
FmpAuthenticationLibPkcs7.c
Go to the documentation of this file.
1
18#include <Uefi.h>
19
22#include <Guid/WinCertificate.h>
23
24#include <Library/BaseLib.h>
26#include <Library/DebugLib.h>
30#include <Library/PcdLib.h>
33
57RETURN_STATUS
60 IN UINTN ImageSize,
61 IN CONST UINT8 *PublicKeyData,
62 IN UINTN PublicKeyDataLength
63 )
64{
65 RETURN_STATUS Status;
66 BOOLEAN CryptoStatus;
67 VOID *P7Data;
68 UINTN P7Length;
69 VOID *TempBuffer;
70
71 DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerPkcs7 - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize));
72
73 P7Length = Image->AuthInfo.Hdr.dwLength - (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData));
74 P7Data = Image->AuthInfo.CertData;
75
76 // It is a signature across the variable data and the Monotonic Count value.
77 TempBuffer = AllocatePool (ImageSize - Image->AuthInfo.Hdr.dwLength);
78 if (TempBuffer == NULL) {
79 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerPkcs7: TempBuffer == NULL\n"));
81 goto Done;
82 }
83
84 CopyMem (
85 TempBuffer,
86 (UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength,
87 ImageSize - sizeof (Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength
88 );
89 CopyMem (
90 (UINT8 *)TempBuffer + ImageSize - sizeof (Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength,
91 &Image->MonotonicCount,
92 sizeof (Image->MonotonicCount)
93 );
94 CryptoStatus = Pkcs7Verify (
95 P7Data,
96 P7Length,
97 PublicKeyData,
98 PublicKeyDataLength,
99 (UINT8 *)TempBuffer,
100 ImageSize - Image->AuthInfo.Hdr.dwLength
101 );
102 FreePool (TempBuffer);
103 if (!CryptoStatus) {
104 //
105 // If PKCS7 signature verification fails, AUTH tested failed bit is set.
106 //
107 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerPkcs7: Pkcs7Verify() failed\n"));
109 goto Done;
110 }
111
112 DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerPkcs7: PASS verification\n"));
113
114 Status = RETURN_SUCCESS;
115
116Done:
117 return Status;
118}
119
155RETURN_STATUS
156EFIAPI
159 IN UINTN ImageSize,
160 IN CONST UINT8 *PublicKeyData,
161 IN UINTN PublicKeyDataLength
162 )
163{
164 GUID *CertType;
165 EFI_STATUS Status;
166
167 if ((Image == NULL) || (ImageSize == 0)) {
168 return RETURN_UNSUPPORTED;
169 }
170
171 if (ImageSize < sizeof (EFI_FIRMWARE_IMAGE_AUTHENTICATION)) {
172 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));
174 }
175
176 if (Image->AuthInfo.Hdr.dwLength <= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {
177 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too small\n"));
179 }
180
181 if ((UINTN)Image->AuthInfo.Hdr.dwLength > MAX_UINTN - sizeof (UINT64)) {
182 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too big\n"));
184 }
185
186 if (ImageSize <= sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) {
187 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));
189 }
190
191 if (Image->AuthInfo.Hdr.wRevision != 0x0200) {
192 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wRevision: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wRevision, (UINTN)0x0200));
194 }
195
196 if (Image->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) {
197 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wCertificateType: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wCertificateType, (UINTN)WIN_CERT_TYPE_EFI_GUID));
199 }
200
201 CertType = &Image->AuthInfo.CertType;
202 DEBUG ((DEBUG_INFO, "AuthenticateFmpImage - CertType: %g\n", CertType));
203
204 if (CompareGuid (&gEfiCertPkcs7Guid, CertType)) {
205 //
206 // Call the match handler to extract raw data for the input section data.
207 //
209 Image,
210 ImageSize,
211 PublicKeyData,
212 PublicKeyDataLength
213 );
214 return Status;
215 }
216
217 //
218 // Not found, the input guided section is not supported.
219 //
220 return RETURN_UNSUPPORTED;
221}
UINT64 UINTN
BOOLEAN EFIAPI Pkcs7Verify(IN CONST UINT8 *P7Data, IN UINTN P7Length, IN CONST UINT8 *TrustedCert, IN UINTN CertLength, IN CONST UINT8 *InData, IN UINTN DataLength)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID EFIAPI FreePool(IN VOID *Buffer)
RETURN_STATUS EFIAPI AuthenticateFmpImage(IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image, IN UINTN ImageSize, IN CONST UINT8 *PublicKeyData, IN UINTN PublicKeyDataLength)
RETURN_STATUS FmpAuthenticatedHandlerPkcs7(IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image, IN UINTN ImageSize, IN CONST UINT8 *PublicKeyData, IN UINTN PublicKeyDataLength)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_OUT_OF_RESOURCES
Definition: Base.h:1114
#define RETURN_SECURITY_VIOLATION
Definition: Base.h:1203
#define RETURN_SUCCESS
Definition: Base.h:1066
#define IN
Definition: Base.h:279
#define OFFSET_OF(TYPE, Field)
Definition: Base.h:758
#define RETURN_INVALID_PARAMETER
Definition: Base.h:1076
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
Definition: Base.h:213