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