]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h
SecurityPkg: enhance secure boot Config Dxe & Time Based AuthVariable.
[mirror_edk2.git] / SecurityPkg / Library / AuthVariableLib / AuthServiceInternal.h
CommitLineData
a6811666
SZ
1/** @file\r
2 The internal header file includes the common header files, defines\r
3 internal structure and functions used by AuthService module.\r
4\r
5 Caution: This module requires additional review when modified.\r
6 This driver will have external input - variable data. It may be input in SMM mode.\r
7 This external input must be validated carefully to avoid security issue like\r
8 buffer overflow, integer overflow.\r
9 Variable attribute should also be checked to avoid authentication bypass.\r
10 The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
11 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory\r
12 may not be modified without authorization. If platform fails to protect these resources,\r
13 the authentication service provided in this driver will be broken, and the behavior is undefined.\r
14\r
c035e373 15Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
a6811666
SZ
16This program and the accompanying materials\r
17are licensed and made available under the terms and conditions of the BSD License\r
18which accompanies this distribution. The full text of the license may be found at\r
19http://opensource.org/licenses/bsd-license.php\r
20\r
21THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
22WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
23\r
24**/\r
25\r
26#ifndef _AUTHSERVICE_INTERNAL_H_\r
27#define _AUTHSERVICE_INTERNAL_H_\r
28\r
29#include <Library/AuthVariableLib.h>\r
30#include <Library/BaseLib.h>\r
31#include <Library/BaseMemoryLib.h>\r
32#include <Library/DebugLib.h>\r
33#include <Library/MemoryAllocationLib.h>\r
34#include <Library/BaseCryptLib.h>\r
35#include <Library/PlatformSecureLib.h>\r
36\r
37#include <Guid/AuthenticatedVariableFormat.h>\r
38#include <Guid/ImageAuthentication.h>\r
39\r
c035e373
ZL
40#define TWO_BYTE_ENCODE 0x82\r
41\r
a6811666
SZ
42///\r
43/// Struct to record signature requirement defined by UEFI spec.\r
44/// For SigHeaderSize and SigDataSize, ((UINT32) ~0) means NO exact length requirement for this field.\r
45///\r
46typedef struct {\r
47 EFI_GUID SigType;\r
48 // Expected SignatureHeader size in Bytes.\r
49 UINT32 SigHeaderSize;\r
50 // Expected SignatureData size in Bytes.\r
51 UINT32 SigDataSize;\r
52} EFI_SIGNATURE_ITEM;\r
53\r
54typedef enum {\r
55 AuthVarTypePk,\r
56 AuthVarTypeKek,\r
57 AuthVarTypePriv,\r
58 AuthVarTypePayload\r
59} AUTHVAR_TYPE;\r
60\r
61///\r
62/// "AuthVarKeyDatabase" variable for the Public Key store\r
63/// of variables with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
64///\r
65/// GUID: gEfiAuthenticatedVariableGuid\r
66///\r
67/// We need maintain atomicity.\r
68///\r
69/// Format:\r
70/// +----------------------------+\r
71/// | AUTHVAR_KEY_DB_DATA | <-- First AuthVarKey\r
72/// +----------------------------+\r
73/// | ...... |\r
74/// +----------------------------+\r
75/// | AUTHVAR_KEY_DB_DATA | <-- Last AuthKey\r
76/// +----------------------------+\r
77///\r
78#define AUTHVAR_KEYDB_NAME L"AuthVarKeyDatabase"\r
79\r
80#define EFI_CERT_TYPE_RSA2048_SHA256_SIZE 256\r
81#define EFI_CERT_TYPE_RSA2048_SIZE 256\r
82\r
83#pragma pack(1)\r
84typedef struct {\r
85 UINT32 KeyIndex;\r
86 UINT8 KeyData[EFI_CERT_TYPE_RSA2048_SIZE];\r
87} AUTHVAR_KEY_DB_DATA;\r
88#pragma pack()\r
89\r
90///\r
98c2d961
CZ
91/// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX\r
92/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS|EFI_VARIABLE_NON_VOLATILE set.\r
93/// "certdbv" variable stores the signer's certificates for non PK/KEK/DB/DBX\r
94/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
a6811666
SZ
95///\r
96/// GUID: gEfiCertDbGuid\r
97///\r
98/// We need maintain atomicity.\r
99///\r
100/// Format:\r
101/// +----------------------------+\r
102/// | UINT32 | <-- CertDbListSize, including this UINT32\r
103/// +----------------------------+\r
104/// | AUTH_CERT_DB_DATA | <-- First CERT\r
105/// +----------------------------+\r
106/// | ........ |\r
107/// +----------------------------+\r
108/// | AUTH_CERT_DB_DATA | <-- Last CERT\r
109/// +----------------------------+\r
110///\r
98c2d961
CZ
111#define EFI_CERT_DB_NAME L"certdb"\r
112#define EFI_CERT_DB_VOLATILE_NAME L"certdbv"\r
a6811666
SZ
113\r
114#pragma pack(1)\r
115typedef struct {\r
116 EFI_GUID VendorGuid;\r
117 UINT32 CertNodeSize;\r
118 UINT32 NameSize;\r
119 UINT32 CertDataSize;\r
120 /// CHAR16 VariableName[NameSize];\r
121 /// UINT8 CertData[CertDataSize];\r
122} AUTH_CERT_DB_DATA;\r
123#pragma pack()\r
124\r
125extern UINT8 *mPubKeyStore;\r
126extern UINT32 mPubKeyNumber;\r
127extern UINT32 mMaxKeyNumber;\r
128extern UINT32 mMaxKeyDbSize;\r
129extern UINT8 *mCertDbStore;\r
130extern UINT32 mMaxCertDbSize;\r
131extern UINT32 mPlatformMode;\r
132extern UINT8 mVendorKeyState;\r
133\r
134extern VOID *mHashCtx;\r
135\r
136extern AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn;\r
137\r
4fc08e8d 138\r
a6811666
SZ
139/**\r
140 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
141\r
142 Caution: This function may receive untrusted input.\r
143 This function may be invoked in SMM mode, and datasize and data are external input.\r
144 This function will do basic validation, before parse the data.\r
145 This function will parse the authentication carefully to avoid security issues, like\r
146 buffer overflow, integer overflow.\r
147\r
148 @param[in] VariableName Name of Variable to be found.\r
149 @param[in] VendorGuid Variable vendor GUID.\r
150 @param[in] Data Data pointer.\r
151 @param[in] DataSize Size of Data found. If size is less than the\r
152 data, this value contains the required size.\r
153 @param[in] Attributes Attribute value of the variable.\r
154 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.\r
155 @param[out] VarDel Delete the variable or not.\r
156\r
157 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
158 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation\r
159 check carried out by the firmware.\r
160 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack\r
161 of resources.\r
162 @retval EFI_SUCCESS Variable pass validation successfully.\r
163\r
164**/\r
165EFI_STATUS\r
166VerifyTimeBasedPayloadAndUpdate (\r
167 IN CHAR16 *VariableName,\r
168 IN EFI_GUID *VendorGuid,\r
169 IN VOID *Data,\r
170 IN UINTN DataSize,\r
171 IN UINT32 Attributes,\r
172 IN AUTHVAR_TYPE AuthVarType,\r
173 OUT BOOLEAN *VarDel\r
174 );\r
175\r
176/**\r
177 Delete matching signer's certificates when deleting common authenticated\r
98c2d961
CZ
178 variable by corresponding VariableName and VendorGuid from "certdb" or \r
179 "certdbv" according to authenticated variable attributes.\r
a6811666
SZ
180\r
181 @param[in] VariableName Name of authenticated Variable.\r
182 @param[in] VendorGuid Vendor GUID of authenticated Variable.\r
98c2d961 183 @param[in] Attributes Attributes of authenticated variable.\r
a6811666
SZ
184\r
185 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
98c2d961 186 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.\r
a6811666
SZ
187 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.\r
188 @retval EFI_SUCCESS The operation is completed successfully.\r
189\r
190**/\r
191EFI_STATUS\r
192DeleteCertsFromDb (\r
193 IN CHAR16 *VariableName,\r
98c2d961
CZ
194 IN EFI_GUID *VendorGuid,\r
195 IN UINT32 Attributes\r
a6811666
SZ
196 );\r
197\r
64b6a3ff
CZ
198/**\r
199 Clean up signer's certificates for common authenticated variable\r
200 by corresponding VariableName and VendorGuid from "certdb".\r
201 Sytem may break down during Timebased Variable update & certdb update,\r
202 make them inconsistent, this function is called in AuthVariable Init to ensure \r
203 consistency\r
204 \r
205 @retval EFI_NOT_FOUND Fail to find matching certs.\r
206 @retval EFI_SUCCESS Find matching certs and output parameters.\r
207\r
208**/\r
209EFI_STATUS\r
210CleanCertsFromDb (\r
211 VOID\r
212 );\r
213\r
a6811666
SZ
214/**\r
215 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.\r
216\r
217 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.\r
218 @param[in] DataSize Size of Data buffer.\r
219 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.\r
220 @param[in, out] NewDataSize Size of NewData buffer.\r
221\r
222**/\r
223EFI_STATUS\r
224FilterSignatureList (\r
225 IN VOID *Data,\r
226 IN UINTN DataSize,\r
227 IN OUT VOID *NewData,\r
228 IN OUT UINTN *NewDataSize\r
229 );\r
230\r
231/**\r
232 Process variable with platform key for verification.\r
233\r
234 Caution: This function may receive untrusted input.\r
235 This function may be invoked in SMM mode, and datasize and data are external input.\r
236 This function will do basic validation, before parse the data.\r
237 This function will parse the authentication carefully to avoid security issues, like\r
238 buffer overflow, integer overflow.\r
239 This function will check attribute carefully to avoid authentication bypass.\r
240\r
241 @param[in] VariableName Name of Variable to be found.\r
242 @param[in] VendorGuid Variable vendor GUID.\r
243 @param[in] Data Data pointer.\r
244 @param[in] DataSize Size of Data found. If size is less than the\r
245 data, this value contains the required size.\r
246 @param[in] Attributes Attribute value of the variable\r
247 @param[in] IsPk Indicate whether it is to process pk.\r
248\r
249 @return EFI_INVALID_PARAMETER Invalid parameter.\r
250 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.\r
251 check carried out by the firmware.\r
252 @return EFI_SUCCESS Variable passed validation successfully.\r
253\r
254**/\r
255EFI_STATUS\r
256ProcessVarWithPk (\r
257 IN CHAR16 *VariableName,\r
258 IN EFI_GUID *VendorGuid,\r
259 IN VOID *Data,\r
260 IN UINTN DataSize,\r
261 IN UINT32 Attributes OPTIONAL,\r
262 IN BOOLEAN IsPk\r
263 );\r
264\r
265/**\r
266 Process variable with key exchange key for verification.\r
267\r
268 Caution: This function may receive untrusted input.\r
269 This function may be invoked in SMM mode, and datasize and data are external input.\r
270 This function will do basic validation, before parse the data.\r
271 This function will parse the authentication carefully to avoid security issues, like\r
272 buffer overflow, integer overflow.\r
273 This function will check attribute carefully to avoid authentication bypass.\r
274\r
275 @param[in] VariableName Name of Variable to be found.\r
276 @param[in] VendorGuid Variable vendor GUID.\r
277 @param[in] Data Data pointer.\r
278 @param[in] DataSize Size of Data found. If size is less than the\r
279 data, this value contains the required size.\r
280 @param[in] Attributes Attribute value of the variable.\r
281\r
282 @return EFI_INVALID_PARAMETER Invalid parameter.\r
283 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation\r
284 check carried out by the firmware.\r
285 @return EFI_SUCCESS Variable pass validation successfully.\r
286\r
287**/\r
288EFI_STATUS\r
289ProcessVarWithKek (\r
290 IN CHAR16 *VariableName,\r
291 IN EFI_GUID *VendorGuid,\r
292 IN VOID *Data,\r
293 IN UINTN DataSize,\r
294 IN UINT32 Attributes OPTIONAL\r
295 );\r
296\r
297/**\r
298 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
299\r
300 Caution: This function may receive untrusted input.\r
301 This function may be invoked in SMM mode, and datasize and data are external input.\r
302 This function will do basic validation, before parse the data.\r
303 This function will parse the authentication carefully to avoid security issues, like\r
304 buffer overflow, integer overflow.\r
305 This function will check attribute carefully to avoid authentication bypass.\r
306\r
307 @param[in] VariableName Name of the variable.\r
308 @param[in] VendorGuid Variable vendor GUID.\r
309 @param[in] Data Data pointer.\r
310 @param[in] DataSize Size of Data.\r
311 @param[in] Attributes Attribute value of the variable.\r
312\r
313 @return EFI_INVALID_PARAMETER Invalid parameter.\r
314 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with\r
315 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
316 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.\r
317 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
318 set, but the AuthInfo does NOT pass the validation\r
319 check carried out by the firmware.\r
320 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.\r
321\r
322**/\r
323EFI_STATUS\r
324ProcessVariable (\r
325 IN CHAR16 *VariableName,\r
326 IN EFI_GUID *VendorGuid,\r
327 IN VOID *Data,\r
328 IN UINTN DataSize,\r
98c2d961 329 IN UINT32 Attributes\r
a6811666
SZ
330 );\r
331\r
332/**\r
333 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
334\r
335 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
336 If VariableName is an empty string, then we just return the first\r
337 qualified variable without comparing VariableName and VendorGuid.\r
338\r
339 @param[in] VariableName Name of the variable to be found.\r
340 @param[in] VendorGuid Variable vendor GUID to be found.\r
341 @param[out] Data Pointer to data address.\r
342 @param[out] DataSize Pointer to data size.\r
343\r
344 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,\r
345 while VendorGuid is NULL.\r
346 @retval EFI_SUCCESS Variable successfully found.\r
347 @retval EFI_NOT_FOUND Variable not found\r
348\r
349**/\r
350EFI_STATUS\r
351AuthServiceInternalFindVariable (\r
352 IN CHAR16 *VariableName,\r
353 IN EFI_GUID *VendorGuid,\r
354 OUT VOID **Data,\r
355 OUT UINTN *DataSize\r
356 );\r
357\r
358/**\r
359 Update the variable region with Variable information.\r
360\r
361 @param[in] VariableName Name of variable.\r
362 @param[in] VendorGuid Guid of variable.\r
363 @param[in] Data Data pointer.\r
364 @param[in] DataSize Size of Data.\r
365 @param[in] Attributes Attribute value of the variable.\r
366\r
367 @retval EFI_SUCCESS The update operation is success.\r
368 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
369 @retval EFI_WRITE_PROTECTED Variable is write-protected.\r
370 @retval EFI_OUT_OF_RESOURCES There is not enough resource.\r
371\r
372**/\r
373EFI_STATUS\r
374AuthServiceInternalUpdateVariable (\r
375 IN CHAR16 *VariableName,\r
376 IN EFI_GUID *VendorGuid,\r
377 IN VOID *Data,\r
378 IN UINTN DataSize,\r
379 IN UINT32 Attributes\r
380 );\r
381\r
382/**\r
383 Update the variable region with Variable information.\r
384\r
385 @param[in] VariableName Name of variable.\r
386 @param[in] VendorGuid Guid of variable.\r
387 @param[in] Data Data pointer.\r
388 @param[in] DataSize Size of Data.\r
389 @param[in] Attributes Attribute value of the variable.\r
390 @param[in] KeyIndex Index of associated public key.\r
391 @param[in] MonotonicCount Value of associated monotonic count.\r
392\r
393 @retval EFI_SUCCESS The update operation is success.\r
394 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
395 @retval EFI_WRITE_PROTECTED Variable is write-protected.\r
396 @retval EFI_OUT_OF_RESOURCES There is not enough resource.\r
397\r
398**/\r
399EFI_STATUS\r
400AuthServiceInternalUpdateVariableWithMonotonicCount (\r
401 IN CHAR16 *VariableName,\r
402 IN EFI_GUID *VendorGuid,\r
403 IN VOID *Data,\r
404 IN UINTN DataSize,\r
405 IN UINT32 Attributes,\r
406 IN UINT32 KeyIndex,\r
407 IN UINT64 MonotonicCount\r
408 );\r
409\r
410/**\r
411 Update the variable region with Variable information.\r
412\r
413 @param[in] VariableName Name of variable.\r
414 @param[in] VendorGuid Guid of variable.\r
415 @param[in] Data Data pointer.\r
416 @param[in] DataSize Size of Data.\r
417 @param[in] Attributes Attribute value of the variable.\r
418 @param[in] TimeStamp Value of associated TimeStamp.\r
419\r
420 @retval EFI_SUCCESS The update operation is success.\r
421 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
422 @retval EFI_WRITE_PROTECTED Variable is write-protected.\r
423 @retval EFI_OUT_OF_RESOURCES There is not enough resource.\r
424\r
425**/\r
426EFI_STATUS\r
427AuthServiceInternalUpdateVariableWithTimeStamp (\r
428 IN CHAR16 *VariableName,\r
429 IN EFI_GUID *VendorGuid,\r
430 IN VOID *Data,\r
431 IN UINTN DataSize,\r
432 IN UINT32 Attributes,\r
433 IN EFI_TIME *TimeStamp\r
434 );\r
435\r
436#endif\r