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