]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h
SecurityPkg: AuthVariableLib: Cache UserPhysicalPresent in AuthVariableLib
[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 extern UINT8 *mPubKeyStore;
124 extern UINT32 mPubKeyNumber;
125 extern UINT32 mMaxKeyNumber;
126 extern UINT32 mMaxKeyDbSize;
127 extern UINT8 *mCertDbStore;
128 extern UINT32 mMaxCertDbSize;
129 extern UINT32 mPlatformMode;
130 extern UINT8 mVendorKeyState;
131 extern BOOLEAN mUserPhysicalPresent;
132
133 extern VOID *mHashCtx;
134
135 extern AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn;
136
137
138 /**
139 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
140
141 Caution: This function may receive untrusted input.
142 This function may be invoked in SMM mode, and datasize and data are external input.
143 This function will do basic validation, before parse the data.
144 This function will parse the authentication carefully to avoid security issues, like
145 buffer overflow, integer overflow.
146
147 @param[in] VariableName Name of Variable to be found.
148 @param[in] VendorGuid Variable vendor GUID.
149 @param[in] Data Data pointer.
150 @param[in] DataSize Size of Data found. If size is less than the
151 data, this value contains the required size.
152 @param[in] Attributes Attribute value of the variable.
153 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
154 @param[out] VarDel Delete the variable or not.
155
156 @retval EFI_INVALID_PARAMETER Invalid parameter.
157 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
158 check carried out by the firmware.
159 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
160 of resources.
161 @retval EFI_SUCCESS Variable pass validation successfully.
162
163 **/
164 EFI_STATUS
165 VerifyTimeBasedPayloadAndUpdate (
166 IN CHAR16 *VariableName,
167 IN EFI_GUID *VendorGuid,
168 IN VOID *Data,
169 IN UINTN DataSize,
170 IN UINT32 Attributes,
171 IN AUTHVAR_TYPE AuthVarType,
172 OUT BOOLEAN *VarDel
173 );
174
175 /**
176 Delete matching signer's certificates when deleting common authenticated
177 variable by corresponding VariableName and VendorGuid from "certdb" or
178 "certdbv" according to authenticated variable attributes.
179
180 @param[in] VariableName Name of authenticated Variable.
181 @param[in] VendorGuid Vendor GUID of authenticated Variable.
182 @param[in] Attributes Attributes of authenticated variable.
183
184 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
185 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
186 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
187 @retval EFI_SUCCESS The operation is completed successfully.
188
189 **/
190 EFI_STATUS
191 DeleteCertsFromDb (
192 IN CHAR16 *VariableName,
193 IN EFI_GUID *VendorGuid,
194 IN UINT32 Attributes
195 );
196
197 /**
198 Clean up signer's certificates for common authenticated variable
199 by corresponding VariableName and VendorGuid from "certdb".
200 Sytem may break down during Timebased Variable update & certdb update,
201 make them inconsistent, this function is called in AuthVariable Init to ensure
202 consistency
203
204 @retval EFI_NOT_FOUND Fail to find matching certs.
205 @retval EFI_SUCCESS Find matching certs and output parameters.
206
207 **/
208 EFI_STATUS
209 CleanCertsFromDb (
210 VOID
211 );
212
213 /**
214 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
215
216 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
217 @param[in] DataSize Size of Data buffer.
218 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
219 @param[in, out] NewDataSize Size of NewData buffer.
220
221 **/
222 EFI_STATUS
223 FilterSignatureList (
224 IN VOID *Data,
225 IN UINTN DataSize,
226 IN OUT VOID *NewData,
227 IN OUT UINTN *NewDataSize
228 );
229
230 /**
231 Process variable with platform key for verification.
232
233 Caution: This function may receive untrusted input.
234 This function may be invoked in SMM mode, and datasize and data are external input.
235 This function will do basic validation, before parse the data.
236 This function will parse the authentication carefully to avoid security issues, like
237 buffer overflow, integer overflow.
238 This function will check attribute carefully to avoid authentication bypass.
239
240 @param[in] VariableName Name of Variable to be found.
241 @param[in] VendorGuid Variable vendor GUID.
242 @param[in] Data Data pointer.
243 @param[in] DataSize Size of Data found. If size is less than the
244 data, this value contains the required size.
245 @param[in] Attributes Attribute value of the variable
246 @param[in] IsPk Indicate whether it is to process pk.
247
248 @return EFI_INVALID_PARAMETER Invalid parameter.
249 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
250 check carried out by the firmware.
251 @return EFI_SUCCESS Variable passed validation successfully.
252
253 **/
254 EFI_STATUS
255 ProcessVarWithPk (
256 IN CHAR16 *VariableName,
257 IN EFI_GUID *VendorGuid,
258 IN VOID *Data,
259 IN UINTN DataSize,
260 IN UINT32 Attributes OPTIONAL,
261 IN BOOLEAN IsPk
262 );
263
264 /**
265 Process variable with key exchange key for verification.
266
267 Caution: This function may receive untrusted input.
268 This function may be invoked in SMM mode, and datasize and data are external input.
269 This function will do basic validation, before parse the data.
270 This function will parse the authentication carefully to avoid security issues, like
271 buffer overflow, integer overflow.
272 This function will check attribute carefully to avoid authentication bypass.
273
274 @param[in] VariableName Name of Variable to be found.
275 @param[in] VendorGuid Variable vendor GUID.
276 @param[in] Data Data pointer.
277 @param[in] DataSize Size of Data found. If size is less than the
278 data, this value contains the required size.
279 @param[in] Attributes Attribute value of the variable.
280
281 @return EFI_INVALID_PARAMETER Invalid parameter.
282 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
283 check carried out by the firmware.
284 @return EFI_SUCCESS Variable pass validation successfully.
285
286 **/
287 EFI_STATUS
288 ProcessVarWithKek (
289 IN CHAR16 *VariableName,
290 IN EFI_GUID *VendorGuid,
291 IN VOID *Data,
292 IN UINTN DataSize,
293 IN UINT32 Attributes OPTIONAL
294 );
295
296 /**
297 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
298
299 Caution: This function may receive untrusted input.
300 This function may be invoked in SMM mode, and datasize and data are external input.
301 This function will do basic validation, before parse the data.
302 This function will parse the authentication carefully to avoid security issues, like
303 buffer overflow, integer overflow.
304 This function will check attribute carefully to avoid authentication bypass.
305
306 @param[in] VariableName Name of the variable.
307 @param[in] VendorGuid Variable vendor GUID.
308 @param[in] Data Data pointer.
309 @param[in] DataSize Size of Data.
310 @param[in] Attributes Attribute value of the variable.
311
312 @return EFI_INVALID_PARAMETER Invalid parameter.
313 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
314 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
315 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
316 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
317 set, but the AuthInfo does NOT pass the validation
318 check carried out by the firmware.
319 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
320
321 **/
322 EFI_STATUS
323 ProcessVariable (
324 IN CHAR16 *VariableName,
325 IN EFI_GUID *VendorGuid,
326 IN VOID *Data,
327 IN UINTN DataSize,
328 IN UINT32 Attributes
329 );
330
331 /**
332 Finds variable in storage blocks of volatile and non-volatile storage areas.
333
334 This code finds variable in storage blocks of volatile and non-volatile storage areas.
335 If VariableName is an empty string, then we just return the first
336 qualified variable without comparing VariableName and VendorGuid.
337
338 @param[in] VariableName Name of the variable to be found.
339 @param[in] VendorGuid Variable vendor GUID to be found.
340 @param[out] Data Pointer to data address.
341 @param[out] DataSize Pointer to data size.
342
343 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
344 while VendorGuid is NULL.
345 @retval EFI_SUCCESS Variable successfully found.
346 @retval EFI_NOT_FOUND Variable not found
347
348 **/
349 EFI_STATUS
350 AuthServiceInternalFindVariable (
351 IN CHAR16 *VariableName,
352 IN EFI_GUID *VendorGuid,
353 OUT VOID **Data,
354 OUT UINTN *DataSize
355 );
356
357 /**
358 Update the variable region with Variable information.
359
360 @param[in] VariableName Name of variable.
361 @param[in] VendorGuid Guid of variable.
362 @param[in] Data Data pointer.
363 @param[in] DataSize Size of Data.
364 @param[in] Attributes Attribute value of the variable.
365
366 @retval EFI_SUCCESS The update operation is success.
367 @retval EFI_INVALID_PARAMETER Invalid parameter.
368 @retval EFI_WRITE_PROTECTED Variable is write-protected.
369 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
370
371 **/
372 EFI_STATUS
373 AuthServiceInternalUpdateVariable (
374 IN CHAR16 *VariableName,
375 IN EFI_GUID *VendorGuid,
376 IN VOID *Data,
377 IN UINTN DataSize,
378 IN UINT32 Attributes
379 );
380
381 /**
382 Update the variable region with Variable information.
383
384 @param[in] VariableName Name of variable.
385 @param[in] VendorGuid Guid of variable.
386 @param[in] Data Data pointer.
387 @param[in] DataSize Size of Data.
388 @param[in] Attributes Attribute value of the variable.
389 @param[in] KeyIndex Index of associated public key.
390 @param[in] MonotonicCount Value of associated monotonic count.
391
392 @retval EFI_SUCCESS The update operation is success.
393 @retval EFI_INVALID_PARAMETER Invalid parameter.
394 @retval EFI_WRITE_PROTECTED Variable is write-protected.
395 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
396
397 **/
398 EFI_STATUS
399 AuthServiceInternalUpdateVariableWithMonotonicCount (
400 IN CHAR16 *VariableName,
401 IN EFI_GUID *VendorGuid,
402 IN VOID *Data,
403 IN UINTN DataSize,
404 IN UINT32 Attributes,
405 IN UINT32 KeyIndex,
406 IN UINT64 MonotonicCount
407 );
408
409 /**
410 Update the variable region with Variable information.
411
412 @param[in] VariableName Name of variable.
413 @param[in] VendorGuid Guid of variable.
414 @param[in] Data Data pointer.
415 @param[in] DataSize Size of Data.
416 @param[in] Attributes Attribute value of the variable.
417 @param[in] TimeStamp Value of associated TimeStamp.
418
419 @retval EFI_SUCCESS The update operation is success.
420 @retval EFI_INVALID_PARAMETER Invalid parameter.
421 @retval EFI_WRITE_PROTECTED Variable is write-protected.
422 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
423
424 **/
425 EFI_STATUS
426 AuthServiceInternalUpdateVariableWithTimeStamp (
427 IN CHAR16 *VariableName,
428 IN EFI_GUID *VendorGuid,
429 IN VOID *Data,
430 IN UINTN DataSize,
431 IN UINT32 Attributes,
432 IN EFI_TIME *TimeStamp
433 );
434
435 #endif