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