]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h
1d495b08a3ae0878739c80ced401aa2676fd5c0a
[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 - 2018, 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 /// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX
63 /// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS|EFI_VARIABLE_NON_VOLATILE set.
64 /// "certdbv" variable stores the signer's certificates for non PK/KEK/DB/DBX
65 /// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
66 ///
67 /// GUID: gEfiCertDbGuid
68 ///
69 /// We need maintain atomicity.
70 ///
71 /// Format:
72 /// +----------------------------+
73 /// | UINT32 | <-- CertDbListSize, including this UINT32
74 /// +----------------------------+
75 /// | AUTH_CERT_DB_DATA | <-- First CERT
76 /// +----------------------------+
77 /// | ........ |
78 /// +----------------------------+
79 /// | AUTH_CERT_DB_DATA | <-- Last CERT
80 /// +----------------------------+
81 ///
82 #define EFI_CERT_DB_NAME L"certdb"
83 #define EFI_CERT_DB_VOLATILE_NAME L"certdbv"
84
85 #pragma pack(1)
86 typedef struct {
87 EFI_GUID VendorGuid;
88 UINT32 CertNodeSize;
89 UINT32 NameSize;
90 UINT32 CertDataSize;
91 /// CHAR16 VariableName[NameSize];
92 /// UINT8 CertData[CertDataSize];
93 } AUTH_CERT_DB_DATA;
94 #pragma pack()
95
96 extern UINT8 *mCertDbStore;
97 extern UINT32 mMaxCertDbSize;
98 extern UINT32 mPlatformMode;
99 extern UINT8 mVendorKeyState;
100
101 extern VOID *mHashCtx;
102
103 extern AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn;
104
105
106 /**
107 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
108
109 Caution: This function may receive untrusted input.
110 This function may be invoked in SMM mode, and datasize and data are external input.
111 This function will do basic validation, before parse the data.
112 This function will parse the authentication carefully to avoid security issues, like
113 buffer overflow, integer overflow.
114
115 @param[in] VariableName Name of Variable to be found.
116 @param[in] VendorGuid Variable vendor GUID.
117 @param[in] Data Data pointer.
118 @param[in] DataSize Size of Data found. If size is less than the
119 data, this value contains the required size.
120 @param[in] Attributes Attribute value of the variable.
121 @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
122 @param[out] VarDel Delete the variable or not.
123
124 @retval EFI_INVALID_PARAMETER Invalid parameter.
125 @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
126 check carried out by the firmware.
127 @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
128 of resources.
129 @retval EFI_SUCCESS Variable pass validation successfully.
130
131 **/
132 EFI_STATUS
133 VerifyTimeBasedPayloadAndUpdate (
134 IN CHAR16 *VariableName,
135 IN EFI_GUID *VendorGuid,
136 IN VOID *Data,
137 IN UINTN DataSize,
138 IN UINT32 Attributes,
139 IN AUTHVAR_TYPE AuthVarType,
140 OUT BOOLEAN *VarDel
141 );
142
143 /**
144 Delete matching signer's certificates when deleting common authenticated
145 variable by corresponding VariableName and VendorGuid from "certdb" or
146 "certdbv" according to authenticated variable attributes.
147
148 @param[in] VariableName Name of authenticated Variable.
149 @param[in] VendorGuid Vendor GUID of authenticated Variable.
150 @param[in] Attributes Attributes of authenticated variable.
151
152 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
153 @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
154 @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
155 @retval EFI_SUCCESS The operation is completed successfully.
156
157 **/
158 EFI_STATUS
159 DeleteCertsFromDb (
160 IN CHAR16 *VariableName,
161 IN EFI_GUID *VendorGuid,
162 IN UINT32 Attributes
163 );
164
165 /**
166 Clean up signer's certificates for common authenticated variable
167 by corresponding VariableName and VendorGuid from "certdb".
168 Sytem may break down during Timebased Variable update & certdb update,
169 make them inconsistent, this function is called in AuthVariable Init to ensure
170 consistency
171
172 @retval EFI_NOT_FOUND Fail to find matching certs.
173 @retval EFI_SUCCESS Find matching certs and output parameters.
174
175 **/
176 EFI_STATUS
177 CleanCertsFromDb (
178 VOID
179 );
180
181 /**
182 Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
183
184 @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
185 @param[in] DataSize Size of Data buffer.
186 @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
187 @param[in, out] NewDataSize Size of NewData buffer.
188
189 **/
190 EFI_STATUS
191 FilterSignatureList (
192 IN VOID *Data,
193 IN UINTN DataSize,
194 IN OUT VOID *NewData,
195 IN OUT UINTN *NewDataSize
196 );
197
198 /**
199 Process variable with platform key for verification.
200
201 Caution: This function may receive untrusted input.
202 This function may be invoked in SMM mode, and datasize and data are external input.
203 This function will do basic validation, before parse the data.
204 This function will parse the authentication carefully to avoid security issues, like
205 buffer overflow, integer overflow.
206 This function will check attribute carefully to avoid authentication bypass.
207
208 @param[in] VariableName Name of Variable to be found.
209 @param[in] VendorGuid Variable vendor GUID.
210 @param[in] Data Data pointer.
211 @param[in] DataSize Size of Data found. If size is less than the
212 data, this value contains the required size.
213 @param[in] Attributes Attribute value of the variable
214 @param[in] IsPk Indicate whether it is to process pk.
215
216 @return EFI_INVALID_PARAMETER Invalid parameter.
217 @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
218 check carried out by the firmware.
219 @return EFI_SUCCESS Variable passed validation successfully.
220
221 **/
222 EFI_STATUS
223 ProcessVarWithPk (
224 IN CHAR16 *VariableName,
225 IN EFI_GUID *VendorGuid,
226 IN VOID *Data,
227 IN UINTN DataSize,
228 IN UINT32 Attributes OPTIONAL,
229 IN BOOLEAN IsPk
230 );
231
232 /**
233 Process variable with key exchange key for verification.
234
235 Caution: This function may receive untrusted input.
236 This function may be invoked in SMM mode, and datasize and data are external input.
237 This function will do basic validation, before parse the data.
238 This function will parse the authentication carefully to avoid security issues, like
239 buffer overflow, integer overflow.
240 This function will check attribute carefully to avoid authentication bypass.
241
242 @param[in] VariableName Name of Variable to be found.
243 @param[in] VendorGuid Variable vendor GUID.
244 @param[in] Data Data pointer.
245 @param[in] DataSize Size of Data found. If size is less than the
246 data, this value contains the required size.
247 @param[in] Attributes Attribute value of the variable.
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 pass validation successfully.
253
254 **/
255 EFI_STATUS
256 ProcessVarWithKek (
257 IN CHAR16 *VariableName,
258 IN EFI_GUID *VendorGuid,
259 IN VOID *Data,
260 IN UINTN DataSize,
261 IN UINT32 Attributes OPTIONAL
262 );
263
264 /**
265 Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
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 the variable.
275 @param[in] VendorGuid Variable vendor GUID.
276 @param[in] Data Data pointer.
277 @param[in] DataSize Size of Data.
278 @param[in] Attributes Attribute value of the variable.
279
280 @return EFI_INVALID_PARAMETER Invalid parameter.
281 @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
282 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
283 @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
284 @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
285 set, but the AuthInfo does NOT pass the validation
286 check carried out by the firmware.
287 @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
288
289 **/
290 EFI_STATUS
291 ProcessVariable (
292 IN CHAR16 *VariableName,
293 IN EFI_GUID *VendorGuid,
294 IN VOID *Data,
295 IN UINTN DataSize,
296 IN UINT32 Attributes
297 );
298
299 /**
300 Finds variable in storage blocks of volatile and non-volatile storage areas.
301
302 This code finds variable in storage blocks of volatile and non-volatile storage areas.
303 If VariableName is an empty string, then we just return the first
304 qualified variable without comparing VariableName and VendorGuid.
305
306 @param[in] VariableName Name of the variable to be found.
307 @param[in] VendorGuid Variable vendor GUID to be found.
308 @param[out] Data Pointer to data address.
309 @param[out] DataSize Pointer to data size.
310
311 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
312 while VendorGuid is NULL.
313 @retval EFI_SUCCESS Variable successfully found.
314 @retval EFI_NOT_FOUND Variable not found
315
316 **/
317 EFI_STATUS
318 AuthServiceInternalFindVariable (
319 IN CHAR16 *VariableName,
320 IN EFI_GUID *VendorGuid,
321 OUT VOID **Data,
322 OUT UINTN *DataSize
323 );
324
325 /**
326 Update the variable region with Variable information.
327
328 @param[in] VariableName Name of variable.
329 @param[in] VendorGuid Guid of variable.
330 @param[in] Data Data pointer.
331 @param[in] DataSize Size of Data.
332 @param[in] Attributes Attribute value of the variable.
333
334 @retval EFI_SUCCESS The update operation is success.
335 @retval EFI_INVALID_PARAMETER Invalid parameter.
336 @retval EFI_WRITE_PROTECTED Variable is write-protected.
337 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
338
339 **/
340 EFI_STATUS
341 AuthServiceInternalUpdateVariable (
342 IN CHAR16 *VariableName,
343 IN EFI_GUID *VendorGuid,
344 IN VOID *Data,
345 IN UINTN DataSize,
346 IN UINT32 Attributes
347 );
348
349 /**
350 Update the variable region with Variable information.
351
352 @param[in] VariableName Name of variable.
353 @param[in] VendorGuid Guid of variable.
354 @param[in] Data Data pointer.
355 @param[in] DataSize Size of Data.
356 @param[in] Attributes Attribute value of the variable.
357 @param[in] TimeStamp Value of associated TimeStamp.
358
359 @retval EFI_SUCCESS The update operation is success.
360 @retval EFI_INVALID_PARAMETER Invalid parameter.
361 @retval EFI_WRITE_PROTECTED Variable is write-protected.
362 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
363
364 **/
365 EFI_STATUS
366 AuthServiceInternalUpdateVariableWithTimeStamp (
367 IN CHAR16 *VariableName,
368 IN EFI_GUID *VendorGuid,
369 IN VOID *Data,
370 IN UINTN DataSize,
371 IN UINT32 Attributes,
372 IN EFI_TIME *TimeStamp
373 );
374
375 #endif