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
;
100 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
102 Caution: This function may receive untrusted input.
103 This function may be invoked in SMM mode, and datasize and data are external input.
104 This function will do basic validation, before parse the data.
105 This function will parse the authentication carefully to avoid security issues, like
106 buffer overflow, integer overflow.
108 @param[in] VariableName Name of Variable to be found.
109 @param[in] VendorGuid Variable vendor GUID.
110 @param[in] Data Data pointer.
111 @param[in] DataSize Size of Data found. If size is less than the
112 data, this value contains the required size.
113 @param[in] Attributes Attribute value of the variable.
114 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
115 @param[out] VarDel Delete the variable or not.
117 @retval EFI_INVALID_PARAMETER Invalid parameter.
118 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
119 check carried out by the firmware.
120 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
122 @retval EFI_SUCCESS Variable pass validation successfully.
126 VerifyTimeBasedPayloadAndUpdate (
127 IN CHAR16
*VariableName
,
128 IN EFI_GUID
*VendorGuid
,
131 IN UINT32 Attributes
,
132 IN AUTHVAR_TYPE AuthVarType
,
137 Delete matching signer's certificates when deleting common authenticated
138 variable by corresponding VariableName and VendorGuid from "certdb" or
139 "certdbv" according to authenticated variable attributes.
141 @param[in] VariableName Name of authenticated Variable.
142 @param[in] VendorGuid Vendor GUID of authenticated Variable.
143 @param[in] Attributes Attributes of authenticated variable.
145 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
146 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
147 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
148 @retval EFI_SUCCESS The operation is completed successfully.
153 IN CHAR16
*VariableName
,
154 IN EFI_GUID
*VendorGuid
,
159 Clean up signer's certificates for common authenticated variable
160 by corresponding VariableName and VendorGuid from "certdb".
161 System may break down during Timebased Variable update & certdb update,
162 make them inconsistent, this function is called in AuthVariable Init to ensure
165 @retval EFI_NOT_FOUND Fail to find matching certs.
166 @retval EFI_SUCCESS Find matching certs and output parameters.
175 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
177 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
178 @param[in] DataSize Size of Data buffer.
179 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
180 @param[in, out] NewDataSize Size of NewData buffer.
184 FilterSignatureList (
187 IN OUT VOID
*NewData
,
188 IN OUT UINTN
*NewDataSize
192 Process variable with platform key for verification.
194 Caution: This function may receive untrusted input.
195 This function may be invoked in SMM mode, and datasize and data are external input.
196 This function will do basic validation, before parse the data.
197 This function will parse the authentication carefully to avoid security issues, like
198 buffer overflow, integer overflow.
199 This function will check attribute carefully to avoid authentication bypass.
201 @param[in] VariableName Name of Variable to be found.
202 @param[in] VendorGuid Variable vendor GUID.
203 @param[in] Data Data pointer.
204 @param[in] DataSize Size of Data found. If size is less than the
205 data, this value contains the required size.
206 @param[in] Attributes Attribute value of the variable
207 @param[in] IsPk Indicate whether it is to process pk.
209 @return EFI_INVALID_PARAMETER Invalid parameter.
210 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
211 check carried out by the firmware.
212 @return EFI_SUCCESS Variable passed validation successfully.
217 IN CHAR16
*VariableName
,
218 IN EFI_GUID
*VendorGuid
,
221 IN UINT32 Attributes OPTIONAL
,
226 Process variable with key exchange key for verification.
228 Caution: This function may receive untrusted input.
229 This function may be invoked in SMM mode, and datasize and data are external input.
230 This function will do basic validation, before parse the data.
231 This function will parse the authentication carefully to avoid security issues, like
232 buffer overflow, integer overflow.
233 This function will check attribute carefully to avoid authentication bypass.
235 @param[in] VariableName Name of Variable to be found.
236 @param[in] VendorGuid Variable vendor GUID.
237 @param[in] Data Data pointer.
238 @param[in] DataSize Size of Data found. If size is less than the
239 data, this value contains the required size.
240 @param[in] Attributes Attribute value of the variable.
242 @return EFI_INVALID_PARAMETER Invalid parameter.
243 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
244 check carried out by the firmware.
245 @return EFI_SUCCESS Variable pass validation successfully.
250 IN CHAR16
*VariableName
,
251 IN EFI_GUID
*VendorGuid
,
254 IN UINT32 Attributes OPTIONAL
258 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
260 Caution: This function may receive untrusted input.
261 This function may be invoked in SMM mode, and datasize and data are external input.
262 This function will do basic validation, before parse the data.
263 This function will parse the authentication carefully to avoid security issues, like
264 buffer overflow, integer overflow.
265 This function will check attribute carefully to avoid authentication bypass.
267 @param[in] VariableName Name of the variable.
268 @param[in] VendorGuid Variable vendor GUID.
269 @param[in] Data Data pointer.
270 @param[in] DataSize Size of Data.
271 @param[in] Attributes Attribute value of the variable.
273 @return EFI_INVALID_PARAMETER Invalid parameter.
274 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
275 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
276 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
277 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
278 set, but the AuthInfo does NOT pass the validation
279 check carried out by the firmware.
280 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
285 IN CHAR16
*VariableName
,
286 IN EFI_GUID
*VendorGuid
,
293 Finds variable in storage blocks of volatile and non-volatile storage areas.
295 This code finds variable in storage blocks of volatile and non-volatile storage areas.
296 If VariableName is an empty string, then we just return the first
297 qualified variable without comparing VariableName and VendorGuid.
299 @param[in] VariableName Name of the variable to be found.
300 @param[in] VendorGuid Variable vendor GUID to be found.
301 @param[out] Data Pointer to data address.
302 @param[out] DataSize Pointer to data size.
304 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
305 while VendorGuid is NULL.
306 @retval EFI_SUCCESS Variable successfully found.
307 @retval EFI_NOT_FOUND Variable not found
311 AuthServiceInternalFindVariable (
312 IN CHAR16
*VariableName
,
313 IN EFI_GUID
*VendorGuid
,
319 Update the variable region with Variable information.
321 @param[in] VariableName Name of variable.
322 @param[in] VendorGuid Guid of variable.
323 @param[in] Data Data pointer.
324 @param[in] DataSize Size of Data.
325 @param[in] Attributes Attribute value of the variable.
327 @retval EFI_SUCCESS The update operation is success.
328 @retval EFI_INVALID_PARAMETER Invalid parameter.
329 @retval EFI_WRITE_PROTECTED Variable is write-protected.
330 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
334 AuthServiceInternalUpdateVariable (
335 IN CHAR16
*VariableName
,
336 IN EFI_GUID
*VendorGuid
,
343 Update the variable region with Variable information.
345 @param[in] VariableName Name of variable.
346 @param[in] VendorGuid Guid of variable.
347 @param[in] Data Data pointer.
348 @param[in] DataSize Size of Data.
349 @param[in] Attributes Attribute value of the variable.
350 @param[in] TimeStamp Value of associated TimeStamp.
352 @retval EFI_SUCCESS The update operation is success.
353 @retval EFI_INVALID_PARAMETER Invalid parameter.
354 @retval EFI_WRITE_PROTECTED Variable is write-protected.
355 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
359 AuthServiceInternalUpdateVariableWithTimeStamp (
360 IN CHAR16
*VariableName
,
361 IN EFI_GUID
*VendorGuid
,
364 IN UINT32 Attributes
,
365 IN EFI_TIME
*TimeStamp