1 /** @file 2 This driver produces Security2 and Security architectural protocol based on SecurityManagementLib. 3 4 Copyright (c) 2006 - 2016, 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 16 #include <Uefi.h> 17 #include <Protocol/Security.h> 18 #include <Protocol/Security2.h> 19 #include <Library/DebugLib.h> 20 #include <Library/UefiBootServicesTableLib.h> 21 #include <Library/UefiDriverEntryPoint.h> 22 #include <Library/SecurityManagementLib.h> 23 #include "Defer3rdPartyImageLoad.h" 24 25 // 26 // Handle for the Security Architectural Protocol instance produced by this driver 27 // 28 EFI_HANDLE mSecurityArchProtocolHandle = NULL; 29 30 /** 31 The EFI_SECURITY_ARCH_PROTOCOL (SAP) is used to abstract platform-specific 32 policy from the DXE core response to an attempt to use a file that returns a 33 given status for the authentication check from the section extraction protocol. 34 35 The possible responses in a given SAP implementation may include locking 36 flash upon failure to authenticate, attestation logging for all signed drivers, 37 and other exception operations. The File parameter allows for possible logging 38 within the SAP of the driver. 39 40 If File is NULL, then EFI_INVALID_PARAMETER is returned. 41 42 If the file specified by File with an authentication status specified by 43 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned. 44 45 If the file specified by File with an authentication status specified by 46 AuthenticationStatus is not safe for the DXE Core to use under any circumstances, 47 then EFI_ACCESS_DENIED is returned. 48 49 If the file specified by File with an authentication status specified by 50 AuthenticationStatus is not safe for the DXE Core to use right now, but it 51 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is 52 returned. 53 54 @param This The EFI_SECURITY_ARCH_PROTOCOL instance. 55 @param AuthenticationStatus 56 This is the authentication type returned from the Section 57 Extraction protocol. See the Section Extraction Protocol 58 Specification for details on this type. 59 @param File This is a pointer to the device path of the file that is 60 being dispatched. This will optionally be used for logging. 61 62 @retval EFI_SUCCESS Do nothing and return success. 63 @retval EFI_INVALID_PARAMETER File is NULL. 64 **/ 65 EFI_STATUS 66 EFIAPI 67 SecurityStubAuthenticateState ( 68 IN CONST EFI_SECURITY_ARCH_PROTOCOL *This, 69 IN UINT32 AuthenticationStatus, 70 IN CONST EFI_DEVICE_PATH_PROTOCOL *File 71 ) 72 { 73 EFI_STATUS Status; 74 75 Status = ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_AUTHENTICATION_STATE, 76 AuthenticationStatus, 77 File, 78 NULL, 79 0, 80 FALSE 81 ); 82 if (Status == EFI_SUCCESS) { 83 Status = ExecuteSecurityHandlers (AuthenticationStatus, File); 84 } 85 86 return Status; 87 } 88 89 /** 90 The DXE Foundation uses this service to measure and/or verify a UEFI image. 91 92 This service abstracts the invocation of Trusted Computing Group (TCG) measured boot, UEFI 93 Secure boot, and UEFI User Identity infrastructure. For the former two, the DXE Foundation 94 invokes the FileAuthentication() with a DevicePath and corresponding image in 95 FileBuffer memory. The TCG measurement code will record the FileBuffer contents into the 96 appropriate PCR. The image verification logic will confirm the integrity and provenance of the 97 image in FileBuffer of length FileSize . The origin of the image will be DevicePath in 98 these cases. 99 If the FileBuffer is NULL, the interface will determine if the DevicePath can be connected 100 in order to support the User Identification policy. 101 102 @param This The EFI_SECURITY2_ARCH_PROTOCOL instance. 103 @param File A pointer to the device path of the file that is 104 being dispatched. This will optionally be used for logging. 105 @param FileBuffer A pointer to the buffer with the UEFI file image. 106 @param FileSize The size of the file. 107 @param BootPolicy A boot policy that was used to call LoadImage() UEFI service. If 108 FileAuthentication() is invoked not from the LoadImage(), 109 BootPolicy must be set to FALSE. 110 111 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL 112 FileBuffer did authenticate, and the platform policy dictates 113 that the DXE Foundation may use the file. 114 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath 115 and non-NULL FileBuffer did authenticate, and the platform 116 policy dictates that the DXE Foundation may execute the image in 117 FileBuffer. 118 @retval EFI_SUCCESS FileBuffer is NULL and current user has permission to start 119 UEFI device drivers on the device path specified by DevicePath. 120 @retval EFI_SECURITY_VIOLATION The file specified by DevicePath and FileBuffer did not 121 authenticate, and the platform policy dictates that the file should be 122 placed in the untrusted state. The image has been added to the file 123 execution table. 124 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not 125 authenticate, and the platform policy dictates that the DXE 126 Foundation many not use File. 127 @retval EFI_SECURITY_VIOLATION FileBuffer is NULL and the user has no 128 permission to start UEFI device drivers on the device path specified 129 by DevicePath. 130 @retval EFI_SECURITY_VIOLATION FileBuffer is not NULL and the user has no permission to load 131 drivers from the device path specified by DevicePath. The 132 image has been added into the list of the deferred images. 133 **/ 134 EFI_STATUS 135 EFIAPI 136 Security2StubAuthenticate ( 137 IN CONST EFI_SECURITY2_ARCH_PROTOCOL *This, 138 IN CONST EFI_DEVICE_PATH_PROTOCOL *File, 139 IN VOID *FileBuffer, 140 IN UINTN FileSize, 141 IN BOOLEAN BootPolicy 142 ) 143 { 144 EFI_STATUS Status; 145 146 if (FileBuffer != NULL) { 147 Status = Defer3rdPartyImageLoad (File, BootPolicy); 148 if (EFI_ERROR (Status)) { 149 return Status; 150 } 151 } 152 153 return ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_VERIFY_IMAGE | 154 EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD | 155 EFI_AUTH_OPERATION_MEASURE_IMAGE | 156 EFI_AUTH_OPERATION_CONNECT_POLICY, 157 0, 158 File, 159 FileBuffer, 160 FileSize, 161 BootPolicy 162 ); 163 } 164 165 // 166 // Security2 and Security Architectural Protocol instance produced by this driver 167 // 168 EFI_SECURITY_ARCH_PROTOCOL mSecurityStub = { 169 SecurityStubAuthenticateState 170 }; 171 172 EFI_SECURITY2_ARCH_PROTOCOL mSecurity2Stub = { 173 Security2StubAuthenticate 174 }; 175 176 /** 177 Installs Security2 and Security Architectural Protocol. 178 179 @param ImageHandle The image handle of this driver. 180 @param SystemTable A pointer to the EFI System Table. 181 182 @retval EFI_SUCCESS Install the sample Security Architectural Protocol successfully. 183 184 **/ 185 EFI_STATUS 186 EFIAPI 187 SecurityStubInitialize ( 188 IN EFI_HANDLE ImageHandle, 189 IN EFI_SYSTEM_TABLE *SystemTable 190 ) 191 { 192 EFI_STATUS Status; 193 194 // 195 // Make sure the Security Architectural Protocol is not already installed in the system 196 // 197 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiSecurity2ArchProtocolGuid); 198 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiSecurityArchProtocolGuid); 199 200 // 201 // Install the Security Architectural Protocol onto a new handle 202 // 203 Status = gBS->InstallMultipleProtocolInterfaces ( 204 &mSecurityArchProtocolHandle, 205 &gEfiSecurity2ArchProtocolGuid, 206 &mSecurity2Stub, 207 &gEfiSecurityArchProtocolGuid, 208 &mSecurityStub, 209 NULL 210 ); 211 ASSERT_EFI_ERROR (Status); 212 213 Defer3rdPartyImageLoadInitialize (); 214 215 return EFI_SUCCESS; 216 } 217