FmpAuthenticationLibPkcs7.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /** @file
  2. FMP Authentication PKCS7 handler.
  3. Provide generic FMP authentication functions for DXE/PEI post memory phase.
  4. Caution: This module requires additional review when modified.
  5. This module will have external input - capsule image.
  6. This external input must be validated carefully to avoid security issue like
  7. buffer overflow, integer overflow.
  8. FmpAuthenticatedHandlerPkcs7(), AuthenticateFmpImage() will receive
  9. untrusted input and do basic validation.
  10. Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
  11. SPDX-License-Identifier: BSD-2-Clause-Patent
  12. **/
  13. #include <Uefi.h>
  14. #include <Guid/SystemResourceTable.h>
  15. #include <Guid/FirmwareContentsSigned.h>
  16. #include <Guid/WinCertificate.h>
  17. #include <Library/BaseLib.h>
  18. #include <Library/BaseMemoryLib.h>
  19. #include <Library/DebugLib.h>
  20. #include <Library/MemoryAllocationLib.h>
  21. #include <Library/BaseCryptLib.h>
  22. #include <Library/FmpAuthenticationLib.h>
  23. #include <Library/PcdLib.h>
  24. #include <Protocol/FirmwareManagement.h>
  25. #include <Guid/SystemResourceTable.h>
  26. /**
  27. The handler is used to do the authentication for FMP capsule based upon
  28. EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  29. Caution: This function may receive untrusted input.
  30. This function assumes the caller AuthenticateFmpImage()
  31. already did basic validation for EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  32. @param[in] Image Points to an FMP authentication image, started from EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  33. @param[in] ImageSize Size of the authentication image in bytes.
  34. @param[in] PublicKeyData The public key data used to validate the signature.
  35. @param[in] PublicKeyDataLength The length of the public key data.
  36. @retval RETURN_SUCCESS Authentication pass.
  37. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_SUCCESS.
  38. @retval RETURN_SECURITY_VIOLATION Authentication fail.
  39. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR.
  40. @retval RETURN_INVALID_PARAMETER The image is in an invalid format.
  41. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.
  42. @retval RETURN_OUT_OF_RESOURCES No Authentication handler associated with CertType.
  43. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES.
  44. **/
  45. RETURN_STATUS
  46. FmpAuthenticatedHandlerPkcs7 (
  47. IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  48. IN UINTN ImageSize,
  49. IN CONST UINT8 *PublicKeyData,
  50. IN UINTN PublicKeyDataLength
  51. )
  52. {
  53. RETURN_STATUS Status;
  54. BOOLEAN CryptoStatus;
  55. VOID *P7Data;
  56. UINTN P7Length;
  57. VOID *TempBuffer;
  58. DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerPkcs7 - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize));
  59. P7Length = Image->AuthInfo.Hdr.dwLength - (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData));
  60. P7Data = Image->AuthInfo.CertData;
  61. // It is a signature across the variable data and the Monotonic Count value.
  62. TempBuffer = AllocatePool (ImageSize - Image->AuthInfo.Hdr.dwLength);
  63. if (TempBuffer == NULL) {
  64. DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerPkcs7: TempBuffer == NULL\n"));
  65. Status = RETURN_OUT_OF_RESOURCES;
  66. goto Done;
  67. }
  68. CopyMem (
  69. TempBuffer,
  70. (UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength,
  71. ImageSize - sizeof (Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength
  72. );
  73. CopyMem (
  74. (UINT8 *)TempBuffer + ImageSize - sizeof (Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength,
  75. &Image->MonotonicCount,
  76. sizeof (Image->MonotonicCount)
  77. );
  78. CryptoStatus = Pkcs7Verify (
  79. P7Data,
  80. P7Length,
  81. PublicKeyData,
  82. PublicKeyDataLength,
  83. (UINT8 *)TempBuffer,
  84. ImageSize - Image->AuthInfo.Hdr.dwLength
  85. );
  86. FreePool (TempBuffer);
  87. if (!CryptoStatus) {
  88. //
  89. // If PKCS7 signature verification fails, AUTH tested failed bit is set.
  90. //
  91. DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerPkcs7: Pkcs7Verify() failed\n"));
  92. Status = RETURN_SECURITY_VIOLATION;
  93. goto Done;
  94. }
  95. DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerPkcs7: PASS verification\n"));
  96. Status = RETURN_SUCCESS;
  97. Done:
  98. return Status;
  99. }
  100. /**
  101. The function is used to do the authentication for FMP capsule based upon
  102. EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  103. The FMP capsule image should start with EFI_FIRMWARE_IMAGE_AUTHENTICATION,
  104. followed by the payload.
  105. If the return status is RETURN_SUCCESS, the caller may continue the rest
  106. FMP update process.
  107. If the return status is NOT RETURN_SUCCESS, the caller should stop the FMP
  108. update process and convert the return status to LastAttemptStatus
  109. to indicate that FMP update fails.
  110. The LastAttemptStatus can be got from ESRT table or via
  111. EFI_FIRMWARE_MANAGEMENT_PROTOCOL.GetImageInfo().
  112. Caution: This function may receive untrusted input.
  113. @param[in] Image Points to an FMP authentication image, started from EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  114. @param[in] ImageSize Size of the authentication image in bytes.
  115. @param[in] PublicKeyData The public key data used to validate the signature.
  116. @param[in] PublicKeyDataLength The length of the public key data.
  117. @retval RETURN_SUCCESS Authentication pass.
  118. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_SUCCESS.
  119. @retval RETURN_SECURITY_VIOLATION Authentication fail.
  120. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR.
  121. @retval RETURN_INVALID_PARAMETER The image is in an invalid format.
  122. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.
  123. @retval RETURN_UNSUPPORTED No Authentication handler associated with CertType.
  124. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.
  125. @retval RETURN_UNSUPPORTED Image or ImageSize is invalid.
  126. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.
  127. @retval RETURN_OUT_OF_RESOURCES No Authentication handler associated with CertType.
  128. The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES.
  129. **/
  130. RETURN_STATUS
  131. EFIAPI
  132. AuthenticateFmpImage (
  133. IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  134. IN UINTN ImageSize,
  135. IN CONST UINT8 *PublicKeyData,
  136. IN UINTN PublicKeyDataLength
  137. )
  138. {
  139. GUID *CertType;
  140. EFI_STATUS Status;
  141. if ((Image == NULL) || (ImageSize == 0)) {
  142. return RETURN_UNSUPPORTED;
  143. }
  144. if (ImageSize < sizeof (EFI_FIRMWARE_IMAGE_AUTHENTICATION)) {
  145. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));
  146. return RETURN_INVALID_PARAMETER;
  147. }
  148. if (Image->AuthInfo.Hdr.dwLength <= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {
  149. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too small\n"));
  150. return RETURN_INVALID_PARAMETER;
  151. }
  152. if ((UINTN)Image->AuthInfo.Hdr.dwLength > MAX_UINTN - sizeof (UINT64)) {
  153. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too big\n"));
  154. return RETURN_INVALID_PARAMETER;
  155. }
  156. if (ImageSize <= sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) {
  157. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));
  158. return RETURN_INVALID_PARAMETER;
  159. }
  160. if (Image->AuthInfo.Hdr.wRevision != 0x0200) {
  161. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wRevision: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wRevision, (UINTN)0x0200));
  162. return RETURN_INVALID_PARAMETER;
  163. }
  164. if (Image->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) {
  165. DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wCertificateType: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wCertificateType, (UINTN)WIN_CERT_TYPE_EFI_GUID));
  166. return RETURN_INVALID_PARAMETER;
  167. }
  168. CertType = &Image->AuthInfo.CertType;
  169. DEBUG ((DEBUG_INFO, "AuthenticateFmpImage - CertType: %g\n", CertType));
  170. if (CompareGuid (&gEfiCertPkcs7Guid, CertType)) {
  171. //
  172. // Call the match handler to extract raw data for the input section data.
  173. //
  174. Status = FmpAuthenticatedHandlerPkcs7 (
  175. Image,
  176. ImageSize,
  177. PublicKeyData,
  178. PublicKeyDataLength
  179. );
  180. return Status;
  181. }
  182. //
  183. // Not found, the input guided section is not supported.
  184. //
  185. return RETURN_UNSUPPORTED;
  186. }