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