2 The internal header file includes the common header files, defines
3 internal structure and functions used by AuthService module.
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. It may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
9 Variable attribute should also be checked to avoid authentication bypass.
10 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
11 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
12 may not be modified without authorization. If platform fails to protect these resources,
13 the authentication service provided in this driver will be broken, and the behavior is undefined.
15 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
16 SPDX-License-Identifier: BSD-2-Clause-Patent
20 #ifndef _AUTHSERVICE_INTERNAL_H_
21 #define _AUTHSERVICE_INTERNAL_H_
23 #include <Library/AuthVariableLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/BaseCryptLib.h>
29 #include <Library/PlatformSecureLib.h>
31 #include <Guid/AuthenticatedVariableFormat.h>
32 #include <Guid/ImageAuthentication.h>
34 #define TWO_BYTE_ENCODE 0x82
37 /// Struct to record signature requirement defined by UEFI spec.
38 /// For SigHeaderSize and SigDataSize, ((UINT32) ~0) means NO exact length requirement for this field.
42 // Expected SignatureHeader size in Bytes.
44 // Expected SignatureData size in Bytes.
56 /// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX
57 /// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS|EFI_VARIABLE_NON_VOLATILE set.
58 /// "certdbv" variable stores the signer's certificates for non PK/KEK/DB/DBX
59 /// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
61 /// GUID: gEfiCertDbGuid
63 /// We need maintain atomicity.
66 /// +----------------------------+
67 /// | UINT32 | <-- CertDbListSize, including this UINT32
68 /// +----------------------------+
69 /// | AUTH_CERT_DB_DATA | <-- First CERT
70 /// +----------------------------+
72 /// +----------------------------+
73 /// | AUTH_CERT_DB_DATA | <-- Last CERT
74 /// +----------------------------+
76 #define EFI_CERT_DB_NAME L"certdb"
77 #define EFI_CERT_DB_VOLATILE_NAME L"certdbv"
85 /// CHAR16 VariableName[NameSize];
86 /// UINT8 CertData[CertDataSize];
90 extern UINT8
*mCertDbStore
;
91 extern UINT32 mMaxCertDbSize
;
92 extern UINT32 mPlatformMode
;
93 extern UINT8 mVendorKeyState
;
95 extern VOID
*mHashCtx
;
97 extern AUTH_VAR_LIB_CONTEXT_IN
*mAuthVarLibContextIn
;
101 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
103 Caution: This function may receive untrusted input.
104 This function may be invoked in SMM mode, and datasize and data are external input.
105 This function will do basic validation, before parse the data.
106 This function will parse the authentication carefully to avoid security issues, like
107 buffer overflow, integer overflow.
109 @param[in] VariableName Name of Variable to be found.
110 @param[in] VendorGuid Variable vendor GUID.
111 @param[in] Data Data pointer.
112 @param[in] DataSize Size of Data found. If size is less than the
113 data, this value contains the required size.
114 @param[in] Attributes Attribute value of the variable.
115 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
116 @param[out] VarDel Delete the variable or not.
118 @retval EFI_INVALID_PARAMETER Invalid parameter.
119 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
120 check carried out by the firmware.
121 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
123 @retval EFI_SUCCESS Variable pass validation successfully.
127 VerifyTimeBasedPayloadAndUpdate (
128 IN CHAR16
*VariableName
,
129 IN EFI_GUID
*VendorGuid
,
132 IN UINT32 Attributes
,
133 IN AUTHVAR_TYPE AuthVarType
,
138 Delete matching signer's certificates when deleting common authenticated
139 variable by corresponding VariableName and VendorGuid from "certdb" or
140 "certdbv" according to authenticated variable attributes.
142 @param[in] VariableName Name of authenticated Variable.
143 @param[in] VendorGuid Vendor GUID of authenticated Variable.
144 @param[in] Attributes Attributes of authenticated variable.
146 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
147 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
148 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
149 @retval EFI_SUCCESS The operation is completed successfully.
154 IN CHAR16
*VariableName
,
155 IN EFI_GUID
*VendorGuid
,
160 Clean up signer's certificates for common authenticated variable
161 by corresponding VariableName and VendorGuid from "certdb".
162 System may break down during Timebased Variable update & certdb update,
163 make them inconsistent, this function is called in AuthVariable Init to ensure
166 @retval EFI_NOT_FOUND Fail to find matching certs.
167 @retval EFI_SUCCESS Find matching certs and output parameters.
176 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
178 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
179 @param[in] DataSize Size of Data buffer.
180 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
181 @param[in, out] NewDataSize Size of NewData buffer.
185 FilterSignatureList (
188 IN OUT VOID
*NewData
,
189 IN OUT UINTN
*NewDataSize
193 Process variable with platform key for verification.
195 Caution: This function may receive untrusted input.
196 This function may be invoked in SMM mode, and datasize and data are external input.
197 This function will do basic validation, before parse the data.
198 This function will parse the authentication carefully to avoid security issues, like
199 buffer overflow, integer overflow.
200 This function will check attribute carefully to avoid authentication bypass.
202 @param[in] VariableName Name of Variable to be found.
203 @param[in] VendorGuid Variable vendor GUID.
204 @param[in] Data Data pointer.
205 @param[in] DataSize Size of Data found. If size is less than the
206 data, this value contains the required size.
207 @param[in] Attributes Attribute value of the variable
208 @param[in] IsPk Indicate whether it is to process pk.
210 @return EFI_INVALID_PARAMETER Invalid parameter.
211 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
212 check carried out by the firmware.
213 @return EFI_SUCCESS Variable passed validation successfully.
218 IN CHAR16
*VariableName
,
219 IN EFI_GUID
*VendorGuid
,
222 IN UINT32 Attributes OPTIONAL
,
227 Process variable with key exchange key for verification.
229 Caution: This function may receive untrusted input.
230 This function may be invoked in SMM mode, and datasize and data are external input.
231 This function will do basic validation, before parse the data.
232 This function will parse the authentication carefully to avoid security issues, like
233 buffer overflow, integer overflow.
234 This function will check attribute carefully to avoid authentication bypass.
236 @param[in] VariableName Name of Variable to be found.
237 @param[in] VendorGuid Variable vendor GUID.
238 @param[in] Data Data pointer.
239 @param[in] DataSize Size of Data found. If size is less than the
240 data, this value contains the required size.
241 @param[in] Attributes Attribute value of the variable.
243 @return EFI_INVALID_PARAMETER Invalid parameter.
244 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
245 check carried out by the firmware.
246 @return EFI_SUCCESS Variable pass validation successfully.
251 IN CHAR16
*VariableName
,
252 IN EFI_GUID
*VendorGuid
,
255 IN UINT32 Attributes OPTIONAL
259 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
261 Caution: This function may receive untrusted input.
262 This function may be invoked in SMM mode, and datasize and data are external input.
263 This function will do basic validation, before parse the data.
264 This function will parse the authentication carefully to avoid security issues, like
265 buffer overflow, integer overflow.
266 This function will check attribute carefully to avoid authentication bypass.
268 @param[in] VariableName Name of the variable.
269 @param[in] VendorGuid Variable vendor GUID.
270 @param[in] Data Data pointer.
271 @param[in] DataSize Size of Data.
272 @param[in] Attributes Attribute value of the variable.
274 @return EFI_INVALID_PARAMETER Invalid parameter.
275 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
276 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
277 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
278 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
279 set, but the AuthInfo does NOT pass the validation
280 check carried out by the firmware.
281 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
286 IN CHAR16
*VariableName
,
287 IN EFI_GUID
*VendorGuid
,
294 Finds variable in storage blocks of volatile and non-volatile storage areas.
296 This code finds variable in storage blocks of volatile and non-volatile storage areas.
297 If VariableName is an empty string, then we just return the first
298 qualified variable without comparing VariableName and VendorGuid.
300 @param[in] VariableName Name of the variable to be found.
301 @param[in] VendorGuid Variable vendor GUID to be found.
302 @param[out] Data Pointer to data address.
303 @param[out] DataSize Pointer to data size.
305 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
306 while VendorGuid is NULL.
307 @retval EFI_SUCCESS Variable successfully found.
308 @retval EFI_NOT_FOUND Variable not found
312 AuthServiceInternalFindVariable (
313 IN CHAR16
*VariableName
,
314 IN EFI_GUID
*VendorGuid
,
320 Update the variable region with Variable information.
322 @param[in] VariableName Name of variable.
323 @param[in] VendorGuid Guid of variable.
324 @param[in] Data Data pointer.
325 @param[in] DataSize Size of Data.
326 @param[in] Attributes Attribute value of the variable.
328 @retval EFI_SUCCESS The update operation is success.
329 @retval EFI_INVALID_PARAMETER Invalid parameter.
330 @retval EFI_WRITE_PROTECTED Variable is write-protected.
331 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
335 AuthServiceInternalUpdateVariable (
336 IN CHAR16
*VariableName
,
337 IN EFI_GUID
*VendorGuid
,
344 Update the variable region with Variable information.
346 @param[in] VariableName Name of variable.
347 @param[in] VendorGuid Guid of variable.
348 @param[in] Data Data pointer.
349 @param[in] DataSize Size of Data.
350 @param[in] Attributes Attribute value of the variable.
351 @param[in] TimeStamp Value of associated TimeStamp.
353 @retval EFI_SUCCESS The update operation is success.
354 @retval EFI_INVALID_PARAMETER Invalid parameter.
355 @retval EFI_WRITE_PROTECTED Variable is write-protected.
356 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
360 AuthServiceInternalUpdateVariableWithTimeStamp (
361 IN CHAR16
*VariableName
,
362 IN EFI_GUID
*VendorGuid
,
365 IN UINT32 Attributes
,
366 IN EFI_TIME
*TimeStamp