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