AuthService.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. /** @file
  2. Implement authentication services for the authenticated variables.
  3. Caution: This module requires additional review when modified.
  4. This driver will have external input - variable data. It may be input in SMM mode.
  5. This external input must be validated carefully to avoid security issue like
  6. buffer overflow, integer overflow.
  7. Variable attribute should also be checked to avoid authentication bypass.
  8. The whole SMM authentication variable design relies on the integrity of flash part and SMM.
  9. which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
  10. may not be modified without authorization. If platform fails to protect these resources,
  11. the authentication service provided in this driver will be broken, and the behavior is undefined.
  12. ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do
  13. variable authentication.
  14. VerifyTimeBasedPayloadAndUpdate() and VerifyCounterBasedPayload() are sub function to do verification.
  15. They will do basic validation for authentication data structure, then call crypto library
  16. to verify the signature.
  17. Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
  18. Copyright (c) Microsoft Corporation.
  19. SPDX-License-Identifier: BSD-2-Clause-Patent
  20. **/
  21. #include "AuthServiceInternal.h"
  22. #include <Protocol/VariablePolicy.h>
  23. #include <Library/VariablePolicyLib.h>
  24. //
  25. // Public Exponent of RSA Key.
  26. //
  27. CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };
  28. CONST UINT8 mSha256OidValue[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01 };
  29. //
  30. // Requirement for different signature type which have been defined in UEFI spec.
  31. // These data are used to perform SignatureList format check while setting PK/KEK variable.
  32. //
  33. EFI_SIGNATURE_ITEM mSupportSigItem[] = {
  34. // {SigType, SigHeaderSize, SigDataSize }
  35. { EFI_CERT_SHA256_GUID, 0, 32 },
  36. { EFI_CERT_RSA2048_GUID, 0, 256 },
  37. { EFI_CERT_RSA2048_SHA256_GUID, 0, 256 },
  38. { EFI_CERT_SHA1_GUID, 0, 20 },
  39. { EFI_CERT_RSA2048_SHA1_GUID, 0, 256 },
  40. { EFI_CERT_X509_GUID, 0, ((UINT32) ~0) },
  41. { EFI_CERT_SHA224_GUID, 0, 28 },
  42. { EFI_CERT_SHA384_GUID, 0, 48 },
  43. { EFI_CERT_SHA512_GUID, 0, 64 },
  44. { EFI_CERT_X509_SHA256_GUID, 0, 48 },
  45. { EFI_CERT_X509_SHA384_GUID, 0, 64 },
  46. { EFI_CERT_X509_SHA512_GUID, 0, 80 }
  47. };
  48. /**
  49. Finds variable in storage blocks of volatile and non-volatile storage areas.
  50. This code finds variable in storage blocks of volatile and non-volatile storage areas.
  51. If VariableName is an empty string, then we just return the first
  52. qualified variable without comparing VariableName and VendorGuid.
  53. @param[in] VariableName Name of the variable to be found.
  54. @param[in] VendorGuid Variable vendor GUID to be found.
  55. @param[out] Data Pointer to data address.
  56. @param[out] DataSize Pointer to data size.
  57. @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
  58. while VendorGuid is NULL.
  59. @retval EFI_SUCCESS Variable successfully found.
  60. @retval EFI_NOT_FOUND Variable not found
  61. **/
  62. EFI_STATUS
  63. AuthServiceInternalFindVariable (
  64. IN CHAR16 *VariableName,
  65. IN EFI_GUID *VendorGuid,
  66. OUT VOID **Data,
  67. OUT UINTN *DataSize
  68. )
  69. {
  70. EFI_STATUS Status;
  71. AUTH_VARIABLE_INFO AuthVariableInfo;
  72. ZeroMem (&AuthVariableInfo, sizeof (AuthVariableInfo));
  73. Status = mAuthVarLibContextIn->FindVariable (
  74. VariableName,
  75. VendorGuid,
  76. &AuthVariableInfo
  77. );
  78. *Data = AuthVariableInfo.Data;
  79. *DataSize = AuthVariableInfo.DataSize;
  80. return Status;
  81. }
  82. /**
  83. Update the variable region with Variable information.
  84. @param[in] VariableName Name of variable.
  85. @param[in] VendorGuid Guid of variable.
  86. @param[in] Data Data pointer.
  87. @param[in] DataSize Size of Data.
  88. @param[in] Attributes Attribute value of the variable.
  89. @retval EFI_SUCCESS The update operation is success.
  90. @retval EFI_INVALID_PARAMETER Invalid parameter.
  91. @retval EFI_WRITE_PROTECTED Variable is write-protected.
  92. @retval EFI_OUT_OF_RESOURCES There is not enough resource.
  93. **/
  94. EFI_STATUS
  95. AuthServiceInternalUpdateVariable (
  96. IN CHAR16 *VariableName,
  97. IN EFI_GUID *VendorGuid,
  98. IN VOID *Data,
  99. IN UINTN DataSize,
  100. IN UINT32 Attributes
  101. )
  102. {
  103. AUTH_VARIABLE_INFO AuthVariableInfo;
  104. ZeroMem (&AuthVariableInfo, sizeof (AuthVariableInfo));
  105. AuthVariableInfo.VariableName = VariableName;
  106. AuthVariableInfo.VendorGuid = VendorGuid;
  107. AuthVariableInfo.Data = Data;
  108. AuthVariableInfo.DataSize = DataSize;
  109. AuthVariableInfo.Attributes = Attributes;
  110. return mAuthVarLibContextIn->UpdateVariable (
  111. &AuthVariableInfo
  112. );
  113. }
  114. /**
  115. Update the variable region with Variable information.
  116. @param[in] VariableName Name of variable.
  117. @param[in] VendorGuid Guid of variable.
  118. @param[in] Data Data pointer.
  119. @param[in] DataSize Size of Data.
  120. @param[in] Attributes Attribute value of the variable.
  121. @param[in] TimeStamp Value of associated TimeStamp.
  122. @retval EFI_SUCCESS The update operation is success.
  123. @retval EFI_INVALID_PARAMETER Invalid parameter.
  124. @retval EFI_WRITE_PROTECTED Variable is write-protected.
  125. @retval EFI_OUT_OF_RESOURCES There is not enough resource.
  126. **/
  127. EFI_STATUS
  128. AuthServiceInternalUpdateVariableWithTimeStamp (
  129. IN CHAR16 *VariableName,
  130. IN EFI_GUID *VendorGuid,
  131. IN VOID *Data,
  132. IN UINTN DataSize,
  133. IN UINT32 Attributes,
  134. IN EFI_TIME *TimeStamp
  135. )
  136. {
  137. EFI_STATUS FindStatus;
  138. VOID *OrgData;
  139. UINTN OrgDataSize;
  140. AUTH_VARIABLE_INFO AuthVariableInfo;
  141. FindStatus = AuthServiceInternalFindVariable (
  142. VariableName,
  143. VendorGuid,
  144. &OrgData,
  145. &OrgDataSize
  146. );
  147. //
  148. // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
  149. //
  150. if (!EFI_ERROR (FindStatus) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {
  151. if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&
  152. ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||
  153. (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))) ||
  154. (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)))
  155. {
  156. //
  157. // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
  158. // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
  159. //
  160. FilterSignatureList (
  161. OrgData,
  162. OrgDataSize,
  163. Data,
  164. &DataSize
  165. );
  166. }
  167. }
  168. ZeroMem (&AuthVariableInfo, sizeof (AuthVariableInfo));
  169. AuthVariableInfo.VariableName = VariableName;
  170. AuthVariableInfo.VendorGuid = VendorGuid;
  171. AuthVariableInfo.Data = Data;
  172. AuthVariableInfo.DataSize = DataSize;
  173. AuthVariableInfo.Attributes = Attributes;
  174. AuthVariableInfo.TimeStamp = TimeStamp;
  175. return mAuthVarLibContextIn->UpdateVariable (
  176. &AuthVariableInfo
  177. );
  178. }
  179. /**
  180. Determine whether this operation needs a physical present user.
  181. @param[in] VariableName Name of the Variable.
  182. @param[in] VendorGuid GUID of the Variable.
  183. @retval TRUE This variable is protected, only a physical present user could set this variable.
  184. @retval FALSE This variable is not protected.
  185. **/
  186. BOOLEAN
  187. NeedPhysicallyPresent (
  188. IN CHAR16 *VariableName,
  189. IN EFI_GUID *VendorGuid
  190. )
  191. {
  192. // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
  193. if (IsVariablePolicyEnabled ()) {
  194. if ( (CompareGuid (VendorGuid, &gEfiSecureBootEnableDisableGuid) && (StrCmp (VariableName, EFI_SECURE_BOOT_ENABLE_NAME) == 0))
  195. || (CompareGuid (VendorGuid, &gEfiCustomModeEnableGuid) && (StrCmp (VariableName, EFI_CUSTOM_MODE_NAME) == 0)))
  196. {
  197. return TRUE;
  198. }
  199. }
  200. return FALSE;
  201. }
  202. /**
  203. Determine whether the platform is operating in Custom Secure Boot mode.
  204. @retval TRUE The platform is operating in Custom mode.
  205. @retval FALSE The platform is operating in Standard mode.
  206. **/
  207. BOOLEAN
  208. InCustomMode (
  209. VOID
  210. )
  211. {
  212. EFI_STATUS Status;
  213. VOID *Data;
  214. UINTN DataSize;
  215. Status = AuthServiceInternalFindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Data, &DataSize);
  216. if (!EFI_ERROR (Status) && (*(UINT8 *)Data == CUSTOM_SECURE_BOOT_MODE)) {
  217. return TRUE;
  218. }
  219. return FALSE;
  220. }
  221. /**
  222. Update platform mode.
  223. @param[in] Mode SETUP_MODE or USER_MODE.
  224. @return EFI_INVALID_PARAMETER Invalid parameter.
  225. @return EFI_SUCCESS Update platform mode successfully.
  226. **/
  227. EFI_STATUS
  228. UpdatePlatformMode (
  229. IN UINT32 Mode
  230. )
  231. {
  232. EFI_STATUS Status;
  233. VOID *Data;
  234. UINTN DataSize;
  235. UINT8 SecureBootMode;
  236. UINT8 SecureBootEnable;
  237. UINTN VariableDataSize;
  238. Status = AuthServiceInternalFindVariable (
  239. EFI_SETUP_MODE_NAME,
  240. &gEfiGlobalVariableGuid,
  241. &Data,
  242. &DataSize
  243. );
  244. if (EFI_ERROR (Status)) {
  245. return Status;
  246. }
  247. //
  248. // Update the value of SetupMode variable by a simple mem copy, this could avoid possible
  249. // variable storage reclaim at runtime.
  250. //
  251. mPlatformMode = (UINT8)Mode;
  252. CopyMem (Data, &mPlatformMode, sizeof (UINT8));
  253. if (mAuthVarLibContextIn->AtRuntime ()) {
  254. //
  255. // SecureBoot Variable indicates whether the platform firmware is operating
  256. // in Secure boot mode (1) or not (0), so we should not change SecureBoot
  257. // Variable in runtime.
  258. //
  259. return Status;
  260. }
  261. //
  262. // Check "SecureBoot" variable's existence.
  263. // If it doesn't exist, firmware has no capability to perform driver signing verification,
  264. // then set "SecureBoot" to 0.
  265. //
  266. Status = AuthServiceInternalFindVariable (
  267. EFI_SECURE_BOOT_MODE_NAME,
  268. &gEfiGlobalVariableGuid,
  269. &Data,
  270. &DataSize
  271. );
  272. //
  273. // If "SecureBoot" variable exists, then check "SetupMode" variable update.
  274. // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.
  275. // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.
  276. //
  277. if (EFI_ERROR (Status)) {
  278. SecureBootMode = SECURE_BOOT_MODE_DISABLE;
  279. } else {
  280. if (mPlatformMode == USER_MODE) {
  281. SecureBootMode = SECURE_BOOT_MODE_ENABLE;
  282. } else if (mPlatformMode == SETUP_MODE) {
  283. SecureBootMode = SECURE_BOOT_MODE_DISABLE;
  284. } else {
  285. return EFI_NOT_FOUND;
  286. }
  287. }
  288. Status = AuthServiceInternalUpdateVariable (
  289. EFI_SECURE_BOOT_MODE_NAME,
  290. &gEfiGlobalVariableGuid,
  291. &SecureBootMode,
  292. sizeof (UINT8),
  293. EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
  294. );
  295. if (EFI_ERROR (Status)) {
  296. return Status;
  297. }
  298. //
  299. // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.
  300. //
  301. Status = AuthServiceInternalFindVariable (
  302. EFI_SECURE_BOOT_ENABLE_NAME,
  303. &gEfiSecureBootEnableDisableGuid,
  304. &Data,
  305. &DataSize
  306. );
  307. if (SecureBootMode == SECURE_BOOT_MODE_ENABLE) {
  308. //
  309. // Create the "SecureBootEnable" variable as secure boot is enabled.
  310. //
  311. SecureBootEnable = SECURE_BOOT_ENABLE;
  312. VariableDataSize = sizeof (SecureBootEnable);
  313. } else {
  314. //
  315. // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"
  316. // variable is not in secure boot state.
  317. //
  318. if (EFI_ERROR (Status)) {
  319. return EFI_SUCCESS;
  320. }
  321. SecureBootEnable = SECURE_BOOT_DISABLE;
  322. VariableDataSize = 0;
  323. }
  324. Status = AuthServiceInternalUpdateVariable (
  325. EFI_SECURE_BOOT_ENABLE_NAME,
  326. &gEfiSecureBootEnableDisableGuid,
  327. &SecureBootEnable,
  328. VariableDataSize,
  329. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
  330. );
  331. return Status;
  332. }
  333. /**
  334. Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.
  335. @param[in] VariableName Name of Variable to be check.
  336. @param[in] VendorGuid Variable vendor GUID.
  337. @param[in] Data Point to the variable data to be checked.
  338. @param[in] DataSize Size of Data.
  339. @return EFI_INVALID_PARAMETER Invalid signature list format.
  340. @return EFI_SUCCESS Passed signature list format check successfully.
  341. **/
  342. EFI_STATUS
  343. CheckSignatureListFormat (
  344. IN CHAR16 *VariableName,
  345. IN EFI_GUID *VendorGuid,
  346. IN VOID *Data,
  347. IN UINTN DataSize
  348. )
  349. {
  350. EFI_SIGNATURE_LIST *SigList;
  351. UINTN SigDataSize;
  352. UINT32 Index;
  353. UINT32 SigCount;
  354. BOOLEAN IsPk;
  355. VOID *RsaContext;
  356. EFI_SIGNATURE_DATA *CertData;
  357. UINTN CertLen;
  358. if (DataSize == 0) {
  359. return EFI_SUCCESS;
  360. }
  361. ASSERT (VariableName != NULL && VendorGuid != NULL && Data != NULL);
  362. if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)) {
  363. IsPk = TRUE;
  364. } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) ||
  365. (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&
  366. ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||
  367. (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))))
  368. {
  369. IsPk = FALSE;
  370. } else {
  371. return EFI_SUCCESS;
  372. }
  373. SigCount = 0;
  374. SigList = (EFI_SIGNATURE_LIST *)Data;
  375. SigDataSize = DataSize;
  376. RsaContext = NULL;
  377. //
  378. // Walk through the input signature list and check the data format.
  379. // If any signature is incorrectly formed, the whole check will fail.
  380. //
  381. while ((SigDataSize > 0) && (SigDataSize >= SigList->SignatureListSize)) {
  382. for (Index = 0; Index < (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM)); Index++ ) {
  383. if (CompareGuid (&SigList->SignatureType, &mSupportSigItem[Index].SigType)) {
  384. //
  385. // The value of SignatureSize should always be 16 (size of SignatureOwner
  386. // component) add the data length according to signature type.
  387. //
  388. if ((mSupportSigItem[Index].SigDataSize != ((UINT32) ~0)) &&
  389. ((SigList->SignatureSize - sizeof (EFI_GUID)) != mSupportSigItem[Index].SigDataSize))
  390. {
  391. return EFI_INVALID_PARAMETER;
  392. }
  393. if ((mSupportSigItem[Index].SigHeaderSize != ((UINT32) ~0)) &&
  394. (SigList->SignatureHeaderSize != mSupportSigItem[Index].SigHeaderSize))
  395. {
  396. return EFI_INVALID_PARAMETER;
  397. }
  398. break;
  399. }
  400. }
  401. if (Index == (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM))) {
  402. //
  403. // Undefined signature type.
  404. //
  405. return EFI_INVALID_PARAMETER;
  406. }
  407. if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {
  408. //
  409. // Try to retrieve the RSA public key from the X.509 certificate.
  410. // If this operation fails, it's not a valid certificate.
  411. //
  412. RsaContext = RsaNew ();
  413. if (RsaContext == NULL) {
  414. return EFI_INVALID_PARAMETER;
  415. }
  416. CertData = (EFI_SIGNATURE_DATA *)((UINT8 *)SigList + sizeof (EFI_SIGNATURE_LIST) + SigList->SignatureHeaderSize);
  417. CertLen = SigList->SignatureSize - sizeof (EFI_GUID);
  418. if (!RsaGetPublicKeyFromX509 (CertData->SignatureData, CertLen, &RsaContext)) {
  419. RsaFree (RsaContext);
  420. return EFI_INVALID_PARAMETER;
  421. }
  422. RsaFree (RsaContext);
  423. }
  424. if ((SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) % SigList->SignatureSize != 0) {
  425. return EFI_INVALID_PARAMETER;
  426. }
  427. SigCount += (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize;
  428. SigDataSize -= SigList->SignatureListSize;
  429. SigList = (EFI_SIGNATURE_LIST *)((UINT8 *)SigList + SigList->SignatureListSize);
  430. }
  431. if (((UINTN)SigList - (UINTN)Data) != DataSize) {
  432. return EFI_INVALID_PARAMETER;
  433. }
  434. if (IsPk && (SigCount > 1)) {
  435. return EFI_INVALID_PARAMETER;
  436. }
  437. return EFI_SUCCESS;
  438. }
  439. /**
  440. Update "VendorKeys" variable to record the out of band secure boot key modification.
  441. @return EFI_SUCCESS Variable is updated successfully.
  442. @return Others Failed to update variable.
  443. **/
  444. EFI_STATUS
  445. VendorKeyIsModified (
  446. VOID
  447. )
  448. {
  449. EFI_STATUS Status;
  450. if (mVendorKeyState == VENDOR_KEYS_MODIFIED) {
  451. return EFI_SUCCESS;
  452. }
  453. mVendorKeyState = VENDOR_KEYS_MODIFIED;
  454. Status = AuthServiceInternalUpdateVariable (
  455. EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
  456. &gEfiVendorKeysNvGuid,
  457. &mVendorKeyState,
  458. sizeof (UINT8),
  459. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
  460. );
  461. if (EFI_ERROR (Status)) {
  462. return Status;
  463. }
  464. return AuthServiceInternalUpdateVariable (
  465. EFI_VENDOR_KEYS_VARIABLE_NAME,
  466. &gEfiGlobalVariableGuid,
  467. &mVendorKeyState,
  468. sizeof (UINT8),
  469. EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
  470. );
  471. }
  472. /**
  473. Process variable with platform key for verification.
  474. Caution: This function may receive untrusted input.
  475. This function may be invoked in SMM mode, and datasize and data are external input.
  476. This function will do basic validation, before parse the data.
  477. This function will parse the authentication carefully to avoid security issues, like
  478. buffer overflow, integer overflow.
  479. This function will check attribute carefully to avoid authentication bypass.
  480. @param[in] VariableName Name of Variable to be found.
  481. @param[in] VendorGuid Variable vendor GUID.
  482. @param[in] Data Data pointer.
  483. @param[in] DataSize Size of Data found. If size is less than the
  484. data, this value contains the required size.
  485. @param[in] Attributes Attribute value of the variable
  486. @param[in] IsPk Indicate whether it is to process pk.
  487. @return EFI_INVALID_PARAMETER Invalid parameter.
  488. @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation.
  489. check carried out by the firmware.
  490. @return EFI_SUCCESS Variable passed validation successfully.
  491. **/
  492. EFI_STATUS
  493. ProcessVarWithPk (
  494. IN CHAR16 *VariableName,
  495. IN EFI_GUID *VendorGuid,
  496. IN VOID *Data,
  497. IN UINTN DataSize,
  498. IN UINT32 Attributes OPTIONAL,
  499. IN BOOLEAN IsPk
  500. )
  501. {
  502. EFI_STATUS Status;
  503. BOOLEAN Del;
  504. UINT8 *Payload;
  505. UINTN PayloadSize;
  506. if (((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) ||
  507. ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0))
  508. {
  509. //
  510. // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
  511. // authenticated variable.
  512. //
  513. return EFI_INVALID_PARAMETER;
  514. }
  515. //
  516. // Init state of Del. State may change due to secure check
  517. //
  518. Del = FALSE;
  519. if ( (InCustomMode () && UserPhysicalPresent ())
  520. || ( (mPlatformMode == SETUP_MODE)
  521. && !(FeaturePcdGet (PcdRequireSelfSignedPk) && IsPk)))
  522. {
  523. Payload = (UINT8 *)Data + AUTHINFO2_SIZE (Data);
  524. PayloadSize = DataSize - AUTHINFO2_SIZE (Data);
  525. if (PayloadSize == 0) {
  526. Del = TRUE;
  527. }
  528. Status = CheckSignatureListFormat (VariableName, VendorGuid, Payload, PayloadSize);
  529. if (EFI_ERROR (Status)) {
  530. return Status;
  531. }
  532. Status = AuthServiceInternalUpdateVariableWithTimeStamp (
  533. VariableName,
  534. VendorGuid,
  535. Payload,
  536. PayloadSize,
  537. Attributes,
  538. &((EFI_VARIABLE_AUTHENTICATION_2 *)Data)->TimeStamp
  539. );
  540. if (EFI_ERROR (Status)) {
  541. return Status;
  542. }
  543. if ( (mPlatformMode != SETUP_MODE)
  544. || (FeaturePcdGet (PcdRequireSelfSignedPk) && IsPk))
  545. {
  546. Status = VendorKeyIsModified ();
  547. }
  548. } else if (mPlatformMode == USER_MODE) {
  549. //
  550. // Verify against X509 Cert in PK database.
  551. //
  552. Status = VerifyTimeBasedPayloadAndUpdate (
  553. VariableName,
  554. VendorGuid,
  555. Data,
  556. DataSize,
  557. Attributes,
  558. AuthVarTypePk,
  559. &Del
  560. );
  561. } else {
  562. //
  563. // Verify against the certificate in data payload.
  564. //
  565. Status = VerifyTimeBasedPayloadAndUpdate (
  566. VariableName,
  567. VendorGuid,
  568. Data,
  569. DataSize,
  570. Attributes,
  571. AuthVarTypePayload,
  572. &Del
  573. );
  574. }
  575. if (!EFI_ERROR (Status) && IsPk) {
  576. if ((mPlatformMode == SETUP_MODE) && !Del) {
  577. //
  578. // If enroll PK in setup mode, need change to user mode.
  579. //
  580. Status = UpdatePlatformMode (USER_MODE);
  581. } else if ((mPlatformMode == USER_MODE) && Del) {
  582. //
  583. // If delete PK in user mode, need change to setup mode.
  584. //
  585. Status = UpdatePlatformMode (SETUP_MODE);
  586. }
  587. }
  588. return Status;
  589. }
  590. /**
  591. Process variable with key exchange key for verification.
  592. Caution: This function may receive untrusted input.
  593. This function may be invoked in SMM mode, and datasize and data are external input.
  594. This function will do basic validation, before parse the data.
  595. This function will parse the authentication carefully to avoid security issues, like
  596. buffer overflow, integer overflow.
  597. This function will check attribute carefully to avoid authentication bypass.
  598. @param[in] VariableName Name of Variable to be found.
  599. @param[in] VendorGuid Variable vendor GUID.
  600. @param[in] Data Data pointer.
  601. @param[in] DataSize Size of Data found. If size is less than the
  602. data, this value contains the required size.
  603. @param[in] Attributes Attribute value of the variable.
  604. @return EFI_INVALID_PARAMETER Invalid parameter.
  605. @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation
  606. check carried out by the firmware.
  607. @return EFI_SUCCESS Variable pass validation successfully.
  608. **/
  609. EFI_STATUS
  610. ProcessVarWithKek (
  611. IN CHAR16 *VariableName,
  612. IN EFI_GUID *VendorGuid,
  613. IN VOID *Data,
  614. IN UINTN DataSize,
  615. IN UINT32 Attributes OPTIONAL
  616. )
  617. {
  618. EFI_STATUS Status;
  619. UINT8 *Payload;
  620. UINTN PayloadSize;
  621. if (((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) ||
  622. ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0))
  623. {
  624. //
  625. // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based
  626. // authenticated variable.
  627. //
  628. return EFI_INVALID_PARAMETER;
  629. }
  630. Status = EFI_SUCCESS;
  631. if ((mPlatformMode == USER_MODE) && !(InCustomMode () && UserPhysicalPresent ())) {
  632. //
  633. // Time-based, verify against X509 Cert KEK.
  634. //
  635. return VerifyTimeBasedPayloadAndUpdate (
  636. VariableName,
  637. VendorGuid,
  638. Data,
  639. DataSize,
  640. Attributes,
  641. AuthVarTypeKek,
  642. NULL
  643. );
  644. } else {
  645. //
  646. // If in setup mode or custom secure boot mode, no authentication needed.
  647. //
  648. Payload = (UINT8 *)Data + AUTHINFO2_SIZE (Data);
  649. PayloadSize = DataSize - AUTHINFO2_SIZE (Data);
  650. Status = CheckSignatureListFormat (VariableName, VendorGuid, Payload, PayloadSize);
  651. if (EFI_ERROR (Status)) {
  652. return Status;
  653. }
  654. Status = AuthServiceInternalUpdateVariableWithTimeStamp (
  655. VariableName,
  656. VendorGuid,
  657. Payload,
  658. PayloadSize,
  659. Attributes,
  660. &((EFI_VARIABLE_AUTHENTICATION_2 *)Data)->TimeStamp
  661. );
  662. if (EFI_ERROR (Status)) {
  663. return Status;
  664. }
  665. if (mPlatformMode != SETUP_MODE) {
  666. Status = VendorKeyIsModified ();
  667. }
  668. }
  669. return Status;
  670. }
  671. /**
  672. Check if it is to delete auth variable.
  673. @param[in] OrgAttributes Original attribute value of the variable.
  674. @param[in] Data Data pointer.
  675. @param[in] DataSize Size of Data.
  676. @param[in] Attributes Attribute value of the variable.
  677. @retval TRUE It is to delete auth variable.
  678. @retval FALSE It is not to delete auth variable.
  679. **/
  680. BOOLEAN
  681. IsDeleteAuthVariable (
  682. IN UINT32 OrgAttributes,
  683. IN VOID *Data,
  684. IN UINTN DataSize,
  685. IN UINT32 Attributes
  686. )
  687. {
  688. BOOLEAN Del;
  689. UINTN PayloadSize;
  690. Del = FALSE;
  691. //
  692. // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
  693. // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,
  694. // SetVariable must be used with attributes matching the existing variable
  695. // and the DataSize set to the size of the AuthInfo descriptor.
  696. //
  697. if ((Attributes == OrgAttributes) &&
  698. ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0))
  699. {
  700. if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
  701. PayloadSize = DataSize - AUTHINFO2_SIZE (Data);
  702. if (PayloadSize == 0) {
  703. Del = TRUE;
  704. }
  705. } else {
  706. PayloadSize = DataSize - AUTHINFO_SIZE;
  707. if (PayloadSize == 0) {
  708. Del = TRUE;
  709. }
  710. }
  711. }
  712. return Del;
  713. }
  714. /**
  715. Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
  716. Caution: This function may receive untrusted input.
  717. This function may be invoked in SMM mode, and datasize and data are external input.
  718. This function will do basic validation, before parse the data.
  719. This function will parse the authentication carefully to avoid security issues, like
  720. buffer overflow, integer overflow.
  721. This function will check attribute carefully to avoid authentication bypass.
  722. @param[in] VariableName Name of the variable.
  723. @param[in] VendorGuid Variable vendor GUID.
  724. @param[in] Data Data pointer.
  725. @param[in] DataSize Size of Data.
  726. @param[in] Attributes Attribute value of the variable.
  727. @return EFI_INVALID_PARAMETER Invalid parameter.
  728. @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with
  729. EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
  730. @return EFI_OUT_OF_RESOURCES The Database to save the public key is full.
  731. @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
  732. set, but the AuthInfo does NOT pass the validation
  733. check carried out by the firmware.
  734. @return EFI_SUCCESS Variable is not write-protected or pass validation successfully.
  735. **/
  736. EFI_STATUS
  737. ProcessVariable (
  738. IN CHAR16 *VariableName,
  739. IN EFI_GUID *VendorGuid,
  740. IN VOID *Data,
  741. IN UINTN DataSize,
  742. IN UINT32 Attributes
  743. )
  744. {
  745. EFI_STATUS Status;
  746. AUTH_VARIABLE_INFO OrgVariableInfo;
  747. Status = EFI_SUCCESS;
  748. ZeroMem (&OrgVariableInfo, sizeof (OrgVariableInfo));
  749. Status = mAuthVarLibContextIn->FindVariable (
  750. VariableName,
  751. VendorGuid,
  752. &OrgVariableInfo
  753. );
  754. // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
  755. if ((!EFI_ERROR (Status)) && IsDeleteAuthVariable (OrgVariableInfo.Attributes, Data, DataSize, Attributes) && (UserPhysicalPresent () || !IsVariablePolicyEnabled ())) {
  756. //
  757. // Allow the delete operation of common authenticated variable(AT or AW) at user physical presence.
  758. //
  759. Status = AuthServiceInternalUpdateVariable (
  760. VariableName,
  761. VendorGuid,
  762. NULL,
  763. 0,
  764. 0
  765. );
  766. if (!EFI_ERROR (Status) && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) {
  767. Status = DeleteCertsFromDb (VariableName, VendorGuid, Attributes);
  768. }
  769. return Status;
  770. }
  771. if (NeedPhysicallyPresent (VariableName, VendorGuid) && !UserPhysicalPresent ()) {
  772. //
  773. // This variable is protected, only physical present user could modify its value.
  774. //
  775. return EFI_SECURITY_VIOLATION;
  776. }
  777. //
  778. if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
  779. //
  780. // Reject Counter Based Auth Variable processing request.
  781. //
  782. return EFI_UNSUPPORTED;
  783. } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
  784. //
  785. // Process Time-based Authenticated variable.
  786. //
  787. return VerifyTimeBasedPayloadAndUpdate (
  788. VariableName,
  789. VendorGuid,
  790. Data,
  791. DataSize,
  792. Attributes,
  793. AuthVarTypePriv,
  794. NULL
  795. );
  796. }
  797. if ((OrgVariableInfo.Data != NULL) &&
  798. ((OrgVariableInfo.Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0))
  799. {
  800. //
  801. // If the variable is already write-protected, it always needs authentication before update.
  802. //
  803. return EFI_WRITE_PROTECTED;
  804. }
  805. //
  806. // Not authenticated variable, just update variable as usual.
  807. //
  808. Status = AuthServiceInternalUpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes);
  809. return Status;
  810. }
  811. /**
  812. Filter out the duplicated EFI_SIGNATURE_DATA from the new data by comparing to the original data.
  813. @param[in] Data Pointer to original EFI_SIGNATURE_LIST.
  814. @param[in] DataSize Size of Data buffer.
  815. @param[in, out] NewData Pointer to new EFI_SIGNATURE_LIST.
  816. @param[in, out] NewDataSize Size of NewData buffer.
  817. **/
  818. EFI_STATUS
  819. FilterSignatureList (
  820. IN VOID *Data,
  821. IN UINTN DataSize,
  822. IN OUT VOID *NewData,
  823. IN OUT UINTN *NewDataSize
  824. )
  825. {
  826. EFI_SIGNATURE_LIST *CertList;
  827. EFI_SIGNATURE_DATA *Cert;
  828. UINTN CertCount;
  829. EFI_SIGNATURE_LIST *NewCertList;
  830. EFI_SIGNATURE_DATA *NewCert;
  831. UINTN NewCertCount;
  832. UINTN Index;
  833. UINTN Index2;
  834. UINTN Size;
  835. UINT8 *Tail;
  836. UINTN CopiedCount;
  837. UINTN SignatureListSize;
  838. BOOLEAN IsNewCert;
  839. UINT8 *TempData;
  840. UINTN TempDataSize;
  841. EFI_STATUS Status;
  842. if (*NewDataSize == 0) {
  843. return EFI_SUCCESS;
  844. }
  845. TempDataSize = *NewDataSize;
  846. Status = mAuthVarLibContextIn->GetScratchBuffer (&TempDataSize, (VOID **)&TempData);
  847. if (EFI_ERROR (Status)) {
  848. return EFI_OUT_OF_RESOURCES;
  849. }
  850. Tail = TempData;
  851. NewCertList = (EFI_SIGNATURE_LIST *)NewData;
  852. while ((*NewDataSize > 0) && (*NewDataSize >= NewCertList->SignatureListSize)) {
  853. NewCert = (EFI_SIGNATURE_DATA *)((UINT8 *)NewCertList + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);
  854. NewCertCount = (NewCertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - NewCertList->SignatureHeaderSize) / NewCertList->SignatureSize;
  855. CopiedCount = 0;
  856. for (Index = 0; Index < NewCertCount; Index++) {
  857. IsNewCert = TRUE;
  858. Size = DataSize;
  859. CertList = (EFI_SIGNATURE_LIST *)Data;
  860. while ((Size > 0) && (Size >= CertList->SignatureListSize)) {
  861. if (CompareGuid (&CertList->SignatureType, &NewCertList->SignatureType) &&
  862. (CertList->SignatureSize == NewCertList->SignatureSize))
  863. {
  864. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  865. CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
  866. for (Index2 = 0; Index2 < CertCount; Index2++) {
  867. //
  868. // Iterate each Signature Data in this Signature List.
  869. //
  870. if (CompareMem (NewCert, Cert, CertList->SignatureSize) == 0) {
  871. IsNewCert = FALSE;
  872. break;
  873. }
  874. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)Cert + CertList->SignatureSize);
  875. }
  876. }
  877. if (!IsNewCert) {
  878. break;
  879. }
  880. Size -= CertList->SignatureListSize;
  881. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  882. }
  883. if (IsNewCert) {
  884. //
  885. // New EFI_SIGNATURE_DATA, keep it.
  886. //
  887. if (CopiedCount == 0) {
  888. //
  889. // Copy EFI_SIGNATURE_LIST header for only once.
  890. //
  891. CopyMem (Tail, NewCertList, sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);
  892. Tail = Tail + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize;
  893. }
  894. CopyMem (Tail, NewCert, NewCertList->SignatureSize);
  895. Tail += NewCertList->SignatureSize;
  896. CopiedCount++;
  897. }
  898. NewCert = (EFI_SIGNATURE_DATA *)((UINT8 *)NewCert + NewCertList->SignatureSize);
  899. }
  900. //
  901. // Update SignatureListSize in the kept EFI_SIGNATURE_LIST.
  902. //
  903. if (CopiedCount != 0) {
  904. SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize + (CopiedCount * NewCertList->SignatureSize);
  905. CertList = (EFI_SIGNATURE_LIST *)(Tail - SignatureListSize);
  906. CertList->SignatureListSize = (UINT32)SignatureListSize;
  907. }
  908. *NewDataSize -= NewCertList->SignatureListSize;
  909. NewCertList = (EFI_SIGNATURE_LIST *)((UINT8 *)NewCertList + NewCertList->SignatureListSize);
  910. }
  911. TempDataSize = (Tail - (UINT8 *)TempData);
  912. CopyMem (NewData, TempData, TempDataSize);
  913. *NewDataSize = TempDataSize;
  914. return EFI_SUCCESS;
  915. }
  916. /**
  917. Compare two EFI_TIME data.
  918. @param FirstTime A pointer to the first EFI_TIME data.
  919. @param SecondTime A pointer to the second EFI_TIME data.
  920. @retval TRUE The FirstTime is not later than the SecondTime.
  921. @retval FALSE The FirstTime is later than the SecondTime.
  922. **/
  923. BOOLEAN
  924. AuthServiceInternalCompareTimeStamp (
  925. IN EFI_TIME *FirstTime,
  926. IN EFI_TIME *SecondTime
  927. )
  928. {
  929. if (FirstTime->Year != SecondTime->Year) {
  930. return (BOOLEAN)(FirstTime->Year < SecondTime->Year);
  931. } else if (FirstTime->Month != SecondTime->Month) {
  932. return (BOOLEAN)(FirstTime->Month < SecondTime->Month);
  933. } else if (FirstTime->Day != SecondTime->Day) {
  934. return (BOOLEAN)(FirstTime->Day < SecondTime->Day);
  935. } else if (FirstTime->Hour != SecondTime->Hour) {
  936. return (BOOLEAN)(FirstTime->Hour < SecondTime->Hour);
  937. } else if (FirstTime->Minute != SecondTime->Minute) {
  938. return (BOOLEAN)(FirstTime->Minute < SecondTime->Minute);
  939. }
  940. return (BOOLEAN)(FirstTime->Second <= SecondTime->Second);
  941. }
  942. /**
  943. Calculate SHA256 digest of SignerCert CommonName + ToplevelCert tbsCertificate
  944. SignerCert and ToplevelCert are inside the signer certificate chain.
  945. @param[in] SignerCert A pointer to SignerCert data.
  946. @param[in] SignerCertSize Length of SignerCert data.
  947. @param[in] TopLevelCert A pointer to TopLevelCert data.
  948. @param[in] TopLevelCertSize Length of TopLevelCert data.
  949. @param[out] Sha256Digest Sha256 digest calculated.
  950. @return EFI_ABORTED Digest process failed.
  951. @return EFI_SUCCESS SHA256 Digest is successfully calculated.
  952. **/
  953. EFI_STATUS
  954. CalculatePrivAuthVarSignChainSHA256Digest (
  955. IN UINT8 *SignerCert,
  956. IN UINTN SignerCertSize,
  957. IN UINT8 *TopLevelCert,
  958. IN UINTN TopLevelCertSize,
  959. OUT UINT8 *Sha256Digest
  960. )
  961. {
  962. UINT8 *TbsCert;
  963. UINTN TbsCertSize;
  964. CHAR8 CertCommonName[128];
  965. UINTN CertCommonNameSize;
  966. BOOLEAN CryptoStatus;
  967. EFI_STATUS Status;
  968. CertCommonNameSize = sizeof (CertCommonName);
  969. //
  970. // Get SignerCert CommonName
  971. //
  972. Status = X509GetCommonName (SignerCert, SignerCertSize, CertCommonName, &CertCommonNameSize);
  973. if (EFI_ERROR (Status)) {
  974. DEBUG ((DEBUG_INFO, "%a Get SignerCert CommonName failed with status %x\n", __FUNCTION__, Status));
  975. return EFI_ABORTED;
  976. }
  977. //
  978. // Get TopLevelCert tbsCertificate
  979. //
  980. if (!X509GetTBSCert (TopLevelCert, TopLevelCertSize, &TbsCert, &TbsCertSize)) {
  981. DEBUG ((DEBUG_INFO, "%a Get Top-level Cert tbsCertificate failed!\n", __FUNCTION__));
  982. return EFI_ABORTED;
  983. }
  984. //
  985. // Digest SignerCert CN + TopLevelCert tbsCertificate
  986. //
  987. ZeroMem (Sha256Digest, SHA256_DIGEST_SIZE);
  988. CryptoStatus = Sha256Init (mHashCtx);
  989. if (!CryptoStatus) {
  990. return EFI_ABORTED;
  991. }
  992. //
  993. // '\0' is forced in CertCommonName. No overflow issue
  994. //
  995. CryptoStatus = Sha256Update (
  996. mHashCtx,
  997. CertCommonName,
  998. AsciiStrLen (CertCommonName)
  999. );
  1000. if (!CryptoStatus) {
  1001. return EFI_ABORTED;
  1002. }
  1003. CryptoStatus = Sha256Update (mHashCtx, TbsCert, TbsCertSize);
  1004. if (!CryptoStatus) {
  1005. return EFI_ABORTED;
  1006. }
  1007. CryptoStatus = Sha256Final (mHashCtx, Sha256Digest);
  1008. if (!CryptoStatus) {
  1009. return EFI_ABORTED;
  1010. }
  1011. return EFI_SUCCESS;
  1012. }
  1013. /**
  1014. Find matching signer's certificates for common authenticated variable
  1015. by corresponding VariableName and VendorGuid from "certdb" or "certdbv".
  1016. The data format of "certdb" or "certdbv":
  1017. //
  1018. // UINT32 CertDbListSize;
  1019. // /// AUTH_CERT_DB_DATA Certs1[];
  1020. // /// AUTH_CERT_DB_DATA Certs2[];
  1021. // /// ...
  1022. // /// AUTH_CERT_DB_DATA Certsn[];
  1023. //
  1024. @param[in] VariableName Name of authenticated Variable.
  1025. @param[in] VendorGuid Vendor GUID of authenticated Variable.
  1026. @param[in] Data Pointer to variable "certdb" or "certdbv".
  1027. @param[in] DataSize Size of variable "certdb" or "certdbv".
  1028. @param[out] CertOffset Offset of matching CertData, from starting of Data.
  1029. @param[out] CertDataSize Length of CertData in bytes.
  1030. @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from
  1031. starting of Data.
  1032. @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes.
  1033. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  1034. @retval EFI_NOT_FOUND Fail to find matching certs.
  1035. @retval EFI_SUCCESS Find matching certs and output parameters.
  1036. **/
  1037. EFI_STATUS
  1038. FindCertsFromDb (
  1039. IN CHAR16 *VariableName,
  1040. IN EFI_GUID *VendorGuid,
  1041. IN UINT8 *Data,
  1042. IN UINTN DataSize,
  1043. OUT UINT32 *CertOffset OPTIONAL,
  1044. OUT UINT32 *CertDataSize OPTIONAL,
  1045. OUT UINT32 *CertNodeOffset OPTIONAL,
  1046. OUT UINT32 *CertNodeSize OPTIONAL
  1047. )
  1048. {
  1049. UINT32 Offset;
  1050. AUTH_CERT_DB_DATA *Ptr;
  1051. UINT32 CertSize;
  1052. UINT32 NameSize;
  1053. UINT32 NodeSize;
  1054. UINT32 CertDbListSize;
  1055. if ((VariableName == NULL) || (VendorGuid == NULL) || (Data == NULL)) {
  1056. return EFI_INVALID_PARAMETER;
  1057. }
  1058. //
  1059. // Check whether DataSize matches recorded CertDbListSize.
  1060. //
  1061. if (DataSize < sizeof (UINT32)) {
  1062. return EFI_INVALID_PARAMETER;
  1063. }
  1064. CertDbListSize = ReadUnaligned32 ((UINT32 *)Data);
  1065. if (CertDbListSize != (UINT32)DataSize) {
  1066. return EFI_INVALID_PARAMETER;
  1067. }
  1068. Offset = sizeof (UINT32);
  1069. //
  1070. // Get corresponding certificates by VendorGuid and VariableName.
  1071. //
  1072. while (Offset < (UINT32)DataSize) {
  1073. Ptr = (AUTH_CERT_DB_DATA *)(Data + Offset);
  1074. //
  1075. // Check whether VendorGuid matches.
  1076. //
  1077. if (CompareGuid (&Ptr->VendorGuid, VendorGuid)) {
  1078. NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);
  1079. NameSize = ReadUnaligned32 (&Ptr->NameSize);
  1080. CertSize = ReadUnaligned32 (&Ptr->CertDataSize);
  1081. if (NodeSize != sizeof (EFI_GUID) + sizeof (UINT32) * 3 + CertSize +
  1082. sizeof (CHAR16) * NameSize)
  1083. {
  1084. return EFI_INVALID_PARAMETER;
  1085. }
  1086. Offset = Offset + sizeof (EFI_GUID) + sizeof (UINT32) * 3;
  1087. //
  1088. // Check whether VariableName matches.
  1089. //
  1090. if ((NameSize == StrLen (VariableName)) &&
  1091. (CompareMem (Data + Offset, VariableName, NameSize * sizeof (CHAR16)) == 0))
  1092. {
  1093. Offset = Offset + NameSize * sizeof (CHAR16);
  1094. if (CertOffset != NULL) {
  1095. *CertOffset = Offset;
  1096. }
  1097. if (CertDataSize != NULL) {
  1098. *CertDataSize = CertSize;
  1099. }
  1100. if (CertNodeOffset != NULL) {
  1101. *CertNodeOffset = (UINT32)((UINT8 *)Ptr - Data);
  1102. }
  1103. if (CertNodeSize != NULL) {
  1104. *CertNodeSize = NodeSize;
  1105. }
  1106. return EFI_SUCCESS;
  1107. } else {
  1108. Offset = Offset + NameSize * sizeof (CHAR16) + CertSize;
  1109. }
  1110. } else {
  1111. NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);
  1112. Offset = Offset + NodeSize;
  1113. }
  1114. }
  1115. return EFI_NOT_FOUND;
  1116. }
  1117. /**
  1118. Retrieve signer's certificates for common authenticated variable
  1119. by corresponding VariableName and VendorGuid from "certdb"
  1120. or "certdbv" according to authenticated variable attributes.
  1121. @param[in] VariableName Name of authenticated Variable.
  1122. @param[in] VendorGuid Vendor GUID of authenticated Variable.
  1123. @param[in] Attributes Attributes of authenticated variable.
  1124. @param[out] CertData Pointer to signer's certificates.
  1125. @param[out] CertDataSize Length of CertData in bytes.
  1126. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  1127. @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
  1128. @retval EFI_SUCCESS Get signer's certificates successfully.
  1129. **/
  1130. EFI_STATUS
  1131. GetCertsFromDb (
  1132. IN CHAR16 *VariableName,
  1133. IN EFI_GUID *VendorGuid,
  1134. IN UINT32 Attributes,
  1135. OUT UINT8 **CertData,
  1136. OUT UINT32 *CertDataSize
  1137. )
  1138. {
  1139. EFI_STATUS Status;
  1140. UINT8 *Data;
  1141. UINTN DataSize;
  1142. UINT32 CertOffset;
  1143. CHAR16 *DbName;
  1144. if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) {
  1145. return EFI_INVALID_PARAMETER;
  1146. }
  1147. if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
  1148. //
  1149. // Get variable "certdb".
  1150. //
  1151. DbName = EFI_CERT_DB_NAME;
  1152. } else {
  1153. //
  1154. // Get variable "certdbv".
  1155. //
  1156. DbName = EFI_CERT_DB_VOLATILE_NAME;
  1157. }
  1158. //
  1159. // Get variable "certdb" or "certdbv".
  1160. //
  1161. Status = AuthServiceInternalFindVariable (
  1162. DbName,
  1163. &gEfiCertDbGuid,
  1164. (VOID **)&Data,
  1165. &DataSize
  1166. );
  1167. if (EFI_ERROR (Status)) {
  1168. return Status;
  1169. }
  1170. if ((DataSize == 0) || (Data == NULL)) {
  1171. ASSERT (FALSE);
  1172. return EFI_NOT_FOUND;
  1173. }
  1174. Status = FindCertsFromDb (
  1175. VariableName,
  1176. VendorGuid,
  1177. Data,
  1178. DataSize,
  1179. &CertOffset,
  1180. CertDataSize,
  1181. NULL,
  1182. NULL
  1183. );
  1184. if (EFI_ERROR (Status)) {
  1185. return Status;
  1186. }
  1187. *CertData = Data + CertOffset;
  1188. return EFI_SUCCESS;
  1189. }
  1190. /**
  1191. Delete matching signer's certificates when deleting common authenticated
  1192. variable by corresponding VariableName and VendorGuid from "certdb" or
  1193. "certdbv" according to authenticated variable attributes.
  1194. @param[in] VariableName Name of authenticated Variable.
  1195. @param[in] VendorGuid Vendor GUID of authenticated Variable.
  1196. @param[in] Attributes Attributes of authenticated variable.
  1197. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  1198. @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs.
  1199. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
  1200. @retval EFI_SUCCESS The operation is completed successfully.
  1201. **/
  1202. EFI_STATUS
  1203. DeleteCertsFromDb (
  1204. IN CHAR16 *VariableName,
  1205. IN EFI_GUID *VendorGuid,
  1206. IN UINT32 Attributes
  1207. )
  1208. {
  1209. EFI_STATUS Status;
  1210. UINT8 *Data;
  1211. UINTN DataSize;
  1212. UINT32 VarAttr;
  1213. UINT32 CertNodeOffset;
  1214. UINT32 CertNodeSize;
  1215. UINT8 *NewCertDb;
  1216. UINT32 NewCertDbSize;
  1217. CHAR16 *DbName;
  1218. if ((VariableName == NULL) || (VendorGuid == NULL)) {
  1219. return EFI_INVALID_PARAMETER;
  1220. }
  1221. if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
  1222. //
  1223. // Get variable "certdb".
  1224. //
  1225. DbName = EFI_CERT_DB_NAME;
  1226. VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1227. } else {
  1228. //
  1229. // Get variable "certdbv".
  1230. //
  1231. DbName = EFI_CERT_DB_VOLATILE_NAME;
  1232. VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1233. }
  1234. Status = AuthServiceInternalFindVariable (
  1235. DbName,
  1236. &gEfiCertDbGuid,
  1237. (VOID **)&Data,
  1238. &DataSize
  1239. );
  1240. if (EFI_ERROR (Status)) {
  1241. return Status;
  1242. }
  1243. if ((DataSize == 0) || (Data == NULL)) {
  1244. ASSERT (FALSE);
  1245. return EFI_NOT_FOUND;
  1246. }
  1247. if (DataSize == sizeof (UINT32)) {
  1248. //
  1249. // There is no certs in "certdb" or "certdbv".
  1250. //
  1251. return EFI_SUCCESS;
  1252. }
  1253. //
  1254. // Get corresponding cert node from "certdb" or "certdbv".
  1255. //
  1256. Status = FindCertsFromDb (
  1257. VariableName,
  1258. VendorGuid,
  1259. Data,
  1260. DataSize,
  1261. NULL,
  1262. NULL,
  1263. &CertNodeOffset,
  1264. &CertNodeSize
  1265. );
  1266. if (EFI_ERROR (Status)) {
  1267. return Status;
  1268. }
  1269. if (DataSize < (CertNodeOffset + CertNodeSize)) {
  1270. return EFI_NOT_FOUND;
  1271. }
  1272. //
  1273. // Construct new data content of variable "certdb" or "certdbv".
  1274. //
  1275. NewCertDbSize = (UINT32)DataSize - CertNodeSize;
  1276. NewCertDb = (UINT8 *)mCertDbStore;
  1277. //
  1278. // Copy the DB entries before deleting node.
  1279. //
  1280. CopyMem (NewCertDb, Data, CertNodeOffset);
  1281. //
  1282. // Update CertDbListSize.
  1283. //
  1284. CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));
  1285. //
  1286. // Copy the DB entries after deleting node.
  1287. //
  1288. if (DataSize > (CertNodeOffset + CertNodeSize)) {
  1289. CopyMem (
  1290. NewCertDb + CertNodeOffset,
  1291. Data + CertNodeOffset + CertNodeSize,
  1292. DataSize - CertNodeOffset - CertNodeSize
  1293. );
  1294. }
  1295. //
  1296. // Set "certdb" or "certdbv".
  1297. //
  1298. Status = AuthServiceInternalUpdateVariable (
  1299. DbName,
  1300. &gEfiCertDbGuid,
  1301. NewCertDb,
  1302. NewCertDbSize,
  1303. VarAttr
  1304. );
  1305. return Status;
  1306. }
  1307. /**
  1308. Insert signer's certificates for common authenticated variable with VariableName
  1309. and VendorGuid in AUTH_CERT_DB_DATA to "certdb" or "certdbv" according to
  1310. time based authenticated variable attributes. CertData is the SHA256 digest of
  1311. SignerCert CommonName + TopLevelCert tbsCertificate.
  1312. @param[in] VariableName Name of authenticated Variable.
  1313. @param[in] VendorGuid Vendor GUID of authenticated Variable.
  1314. @param[in] Attributes Attributes of authenticated variable.
  1315. @param[in] SignerCert Signer certificate data.
  1316. @param[in] SignerCertSize Length of signer certificate.
  1317. @param[in] TopLevelCert Top-level certificate data.
  1318. @param[in] TopLevelCertSize Length of top-level certificate.
  1319. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  1320. @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName
  1321. and VendorGuid already exists.
  1322. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
  1323. @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" or "certdbv"
  1324. **/
  1325. EFI_STATUS
  1326. InsertCertsToDb (
  1327. IN CHAR16 *VariableName,
  1328. IN EFI_GUID *VendorGuid,
  1329. IN UINT32 Attributes,
  1330. IN UINT8 *SignerCert,
  1331. IN UINTN SignerCertSize,
  1332. IN UINT8 *TopLevelCert,
  1333. IN UINTN TopLevelCertSize
  1334. )
  1335. {
  1336. EFI_STATUS Status;
  1337. UINT8 *Data;
  1338. UINTN DataSize;
  1339. UINT32 VarAttr;
  1340. UINT8 *NewCertDb;
  1341. UINT32 NewCertDbSize;
  1342. UINT32 CertNodeSize;
  1343. UINT32 NameSize;
  1344. UINT32 CertDataSize;
  1345. AUTH_CERT_DB_DATA *Ptr;
  1346. CHAR16 *DbName;
  1347. UINT8 Sha256Digest[SHA256_DIGEST_SIZE];
  1348. if ((VariableName == NULL) || (VendorGuid == NULL) || (SignerCert == NULL) || (TopLevelCert == NULL)) {
  1349. return EFI_INVALID_PARAMETER;
  1350. }
  1351. if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
  1352. //
  1353. // Get variable "certdb".
  1354. //
  1355. DbName = EFI_CERT_DB_NAME;
  1356. VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1357. } else {
  1358. //
  1359. // Get variable "certdbv".
  1360. //
  1361. DbName = EFI_CERT_DB_VOLATILE_NAME;
  1362. VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1363. }
  1364. //
  1365. // Get variable "certdb" or "certdbv".
  1366. //
  1367. Status = AuthServiceInternalFindVariable (
  1368. DbName,
  1369. &gEfiCertDbGuid,
  1370. (VOID **)&Data,
  1371. &DataSize
  1372. );
  1373. if (EFI_ERROR (Status)) {
  1374. return Status;
  1375. }
  1376. if ((DataSize == 0) || (Data == NULL)) {
  1377. ASSERT (FALSE);
  1378. return EFI_NOT_FOUND;
  1379. }
  1380. //
  1381. // Find whether matching cert node already exists in "certdb" or "certdbv".
  1382. // If yes return error.
  1383. //
  1384. Status = FindCertsFromDb (
  1385. VariableName,
  1386. VendorGuid,
  1387. Data,
  1388. DataSize,
  1389. NULL,
  1390. NULL,
  1391. NULL,
  1392. NULL
  1393. );
  1394. if (!EFI_ERROR (Status)) {
  1395. ASSERT (FALSE);
  1396. return EFI_ACCESS_DENIED;
  1397. }
  1398. //
  1399. // Construct new data content of variable "certdb" or "certdbv".
  1400. //
  1401. NameSize = (UINT32)StrLen (VariableName);
  1402. CertDataSize = sizeof (Sha256Digest);
  1403. CertNodeSize = sizeof (AUTH_CERT_DB_DATA) + (UINT32)CertDataSize + NameSize * sizeof (CHAR16);
  1404. NewCertDbSize = (UINT32)DataSize + CertNodeSize;
  1405. if (NewCertDbSize > mMaxCertDbSize) {
  1406. return EFI_OUT_OF_RESOURCES;
  1407. }
  1408. Status = CalculatePrivAuthVarSignChainSHA256Digest (
  1409. SignerCert,
  1410. SignerCertSize,
  1411. TopLevelCert,
  1412. TopLevelCertSize,
  1413. Sha256Digest
  1414. );
  1415. if (EFI_ERROR (Status)) {
  1416. return Status;
  1417. }
  1418. NewCertDb = (UINT8 *)mCertDbStore;
  1419. //
  1420. // Copy the DB entries before inserting node.
  1421. //
  1422. CopyMem (NewCertDb, Data, DataSize);
  1423. //
  1424. // Update CertDbListSize.
  1425. //
  1426. CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));
  1427. //
  1428. // Construct new cert node.
  1429. //
  1430. Ptr = (AUTH_CERT_DB_DATA *)(NewCertDb + DataSize);
  1431. CopyGuid (&Ptr->VendorGuid, VendorGuid);
  1432. CopyMem (&Ptr->CertNodeSize, &CertNodeSize, sizeof (UINT32));
  1433. CopyMem (&Ptr->NameSize, &NameSize, sizeof (UINT32));
  1434. CopyMem (&Ptr->CertDataSize, &CertDataSize, sizeof (UINT32));
  1435. CopyMem (
  1436. (UINT8 *)Ptr + sizeof (AUTH_CERT_DB_DATA),
  1437. VariableName,
  1438. NameSize * sizeof (CHAR16)
  1439. );
  1440. CopyMem (
  1441. (UINT8 *)Ptr + sizeof (AUTH_CERT_DB_DATA) + NameSize * sizeof (CHAR16),
  1442. Sha256Digest,
  1443. CertDataSize
  1444. );
  1445. //
  1446. // Set "certdb" or "certdbv".
  1447. //
  1448. Status = AuthServiceInternalUpdateVariable (
  1449. DbName,
  1450. &gEfiCertDbGuid,
  1451. NewCertDb,
  1452. NewCertDbSize,
  1453. VarAttr
  1454. );
  1455. return Status;
  1456. }
  1457. /**
  1458. Clean up signer's certificates for common authenticated variable
  1459. by corresponding VariableName and VendorGuid from "certdb".
  1460. System may break down during Timebased Variable update & certdb update,
  1461. make them inconsistent, this function is called in AuthVariable Init
  1462. to ensure consistency.
  1463. @retval EFI_NOT_FOUND Fail to find variable "certdb".
  1464. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
  1465. @retval EFI_SUCCESS The operation is completed successfully.
  1466. **/
  1467. EFI_STATUS
  1468. CleanCertsFromDb (
  1469. VOID
  1470. )
  1471. {
  1472. UINT32 Offset;
  1473. AUTH_CERT_DB_DATA *Ptr;
  1474. UINT32 NameSize;
  1475. UINT32 NodeSize;
  1476. CHAR16 *VariableName;
  1477. EFI_STATUS Status;
  1478. BOOLEAN CertCleaned;
  1479. UINT8 *Data;
  1480. UINTN DataSize;
  1481. EFI_GUID AuthVarGuid;
  1482. AUTH_VARIABLE_INFO AuthVariableInfo;
  1483. Status = EFI_SUCCESS;
  1484. //
  1485. // Get corresponding certificates by VendorGuid and VariableName.
  1486. //
  1487. do {
  1488. CertCleaned = FALSE;
  1489. //
  1490. // Get latest variable "certdb"
  1491. //
  1492. Status = AuthServiceInternalFindVariable (
  1493. EFI_CERT_DB_NAME,
  1494. &gEfiCertDbGuid,
  1495. (VOID **)&Data,
  1496. &DataSize
  1497. );
  1498. if (EFI_ERROR (Status)) {
  1499. return Status;
  1500. }
  1501. if ((DataSize == 0) || (Data == NULL)) {
  1502. ASSERT (FALSE);
  1503. return EFI_NOT_FOUND;
  1504. }
  1505. Offset = sizeof (UINT32);
  1506. while (Offset < (UINT32)DataSize) {
  1507. Ptr = (AUTH_CERT_DB_DATA *)(Data + Offset);
  1508. NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);
  1509. NameSize = ReadUnaligned32 (&Ptr->NameSize);
  1510. //
  1511. // Get VarName tailed with '\0'
  1512. //
  1513. VariableName = AllocateZeroPool ((NameSize + 1) * sizeof (CHAR16));
  1514. if (VariableName == NULL) {
  1515. return EFI_OUT_OF_RESOURCES;
  1516. }
  1517. CopyMem (VariableName, (UINT8 *)Ptr + sizeof (AUTH_CERT_DB_DATA), NameSize * sizeof (CHAR16));
  1518. //
  1519. // Keep VarGuid aligned
  1520. //
  1521. CopyMem (&AuthVarGuid, &Ptr->VendorGuid, sizeof (EFI_GUID));
  1522. //
  1523. // Find corresponding time auth variable
  1524. //
  1525. ZeroMem (&AuthVariableInfo, sizeof (AuthVariableInfo));
  1526. Status = mAuthVarLibContextIn->FindVariable (
  1527. VariableName,
  1528. &AuthVarGuid,
  1529. &AuthVariableInfo
  1530. );
  1531. if (EFI_ERROR (Status) || ((AuthVariableInfo.Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0)) {
  1532. //
  1533. // While cleaning certdb, always delete the variable in certdb regardless of it attributes.
  1534. //
  1535. Status = DeleteCertsFromDb (
  1536. VariableName,
  1537. &AuthVarGuid,
  1538. AuthVariableInfo.Attributes | EFI_VARIABLE_NON_VOLATILE
  1539. );
  1540. CertCleaned = TRUE;
  1541. DEBUG ((DEBUG_INFO, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName, &AuthVarGuid));
  1542. FreePool (VariableName);
  1543. break;
  1544. }
  1545. FreePool (VariableName);
  1546. Offset = Offset + NodeSize;
  1547. }
  1548. } while (CertCleaned);
  1549. return Status;
  1550. }
  1551. /**
  1552. Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
  1553. Caution: This function may receive untrusted input.
  1554. This function may be invoked in SMM mode, and datasize and data are external input.
  1555. This function will do basic validation, before parse the data.
  1556. This function will parse the authentication carefully to avoid security issues, like
  1557. buffer overflow, integer overflow.
  1558. @param[in] VariableName Name of Variable to be found.
  1559. @param[in] VendorGuid Variable vendor GUID.
  1560. @param[in] Data Data pointer.
  1561. @param[in] DataSize Size of Data found. If size is less than the
  1562. data, this value contains the required size.
  1563. @param[in] Attributes Attribute value of the variable.
  1564. @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
  1565. @param[in] OrgTimeStamp Pointer to original time stamp,
  1566. original variable is not found if NULL.
  1567. @param[out] VarPayloadPtr Pointer to variable payload address.
  1568. @param[out] VarPayloadSize Pointer to variable payload size.
  1569. @retval EFI_INVALID_PARAMETER Invalid parameter.
  1570. @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
  1571. check carried out by the firmware.
  1572. @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
  1573. of resources.
  1574. @retval EFI_SUCCESS Variable pass validation successfully.
  1575. **/
  1576. EFI_STATUS
  1577. VerifyTimeBasedPayload (
  1578. IN CHAR16 *VariableName,
  1579. IN EFI_GUID *VendorGuid,
  1580. IN VOID *Data,
  1581. IN UINTN DataSize,
  1582. IN UINT32 Attributes,
  1583. IN AUTHVAR_TYPE AuthVarType,
  1584. IN EFI_TIME *OrgTimeStamp,
  1585. OUT UINT8 **VarPayloadPtr,
  1586. OUT UINTN *VarPayloadSize
  1587. )
  1588. {
  1589. EFI_VARIABLE_AUTHENTICATION_2 *CertData;
  1590. UINT8 *SigData;
  1591. UINT32 SigDataSize;
  1592. UINT8 *PayloadPtr;
  1593. UINTN PayloadSize;
  1594. UINT32 Attr;
  1595. BOOLEAN VerifyStatus;
  1596. EFI_STATUS Status;
  1597. EFI_SIGNATURE_LIST *CertList;
  1598. EFI_SIGNATURE_DATA *Cert;
  1599. UINTN Index;
  1600. UINTN CertCount;
  1601. UINT32 KekDataSize;
  1602. UINT8 *NewData;
  1603. UINTN NewDataSize;
  1604. UINT8 *Buffer;
  1605. UINTN Length;
  1606. UINT8 *TopLevelCert;
  1607. UINTN TopLevelCertSize;
  1608. UINT8 *TrustedCert;
  1609. UINTN TrustedCertSize;
  1610. UINT8 *SignerCerts;
  1611. UINTN CertStackSize;
  1612. UINT8 *CertsInCertDb;
  1613. UINT32 CertsSizeinDb;
  1614. UINT8 Sha256Digest[SHA256_DIGEST_SIZE];
  1615. EFI_CERT_DATA *CertDataPtr;
  1616. //
  1617. // 1. TopLevelCert is the top-level issuer certificate in signature Signer Cert Chain
  1618. // 2. TrustedCert is the certificate which firmware trusts. It could be saved in protected
  1619. // storage or PK payload on PK init
  1620. //
  1621. VerifyStatus = FALSE;
  1622. CertData = NULL;
  1623. NewData = NULL;
  1624. Attr = Attributes;
  1625. SignerCerts = NULL;
  1626. TopLevelCert = NULL;
  1627. CertsInCertDb = NULL;
  1628. CertDataPtr = NULL;
  1629. //
  1630. // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is
  1631. // set, then the Data buffer shall begin with an instance of a complete (and serialized)
  1632. // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new
  1633. // variable value and DataSize shall reflect the combined size of the descriptor and the new
  1634. // variable value. The authentication descriptor is not part of the variable data and is not
  1635. // returned by subsequent calls to GetVariable().
  1636. //
  1637. CertData = (EFI_VARIABLE_AUTHENTICATION_2 *)Data;
  1638. //
  1639. // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the
  1640. // TimeStamp value are set to zero.
  1641. //
  1642. if ((CertData->TimeStamp.Pad1 != 0) ||
  1643. (CertData->TimeStamp.Nanosecond != 0) ||
  1644. (CertData->TimeStamp.TimeZone != 0) ||
  1645. (CertData->TimeStamp.Daylight != 0) ||
  1646. (CertData->TimeStamp.Pad2 != 0))
  1647. {
  1648. return EFI_SECURITY_VIOLATION;
  1649. }
  1650. if ((OrgTimeStamp != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) {
  1651. if (AuthServiceInternalCompareTimeStamp (&CertData->TimeStamp, OrgTimeStamp)) {
  1652. //
  1653. // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
  1654. //
  1655. return EFI_SECURITY_VIOLATION;
  1656. }
  1657. }
  1658. //
  1659. // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
  1660. // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.
  1661. //
  1662. if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||
  1663. !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertPkcs7Guid))
  1664. {
  1665. //
  1666. // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
  1667. //
  1668. return EFI_SECURITY_VIOLATION;
  1669. }
  1670. //
  1671. // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.
  1672. // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.
  1673. //
  1674. SigData = CertData->AuthInfo.CertData;
  1675. SigDataSize = CertData->AuthInfo.Hdr.dwLength - (UINT32)(OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData));
  1676. //
  1677. // SignedData.digestAlgorithms shall contain the digest algorithm used when preparing the
  1678. // signature. Only a digest algorithm of SHA-256 is accepted.
  1679. //
  1680. // According to PKCS#7 Definition (https://www.rfc-editor.org/rfc/rfc2315):
  1681. // SignedData ::= SEQUENCE {
  1682. // version Version,
  1683. // digestAlgorithms DigestAlgorithmIdentifiers,
  1684. // contentInfo ContentInfo,
  1685. // .... }
  1686. // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm
  1687. // in VARIABLE_AUTHENTICATION_2 descriptor.
  1688. // This field has the fixed offset (+13) or (+32) based on whether the DER-encoded
  1689. // ContentInfo structure is present or not, and can be calculated based on two
  1690. // bytes of length encoding.
  1691. //
  1692. // Both condition can be handled in WrapPkcs7Data() in CryptPkcs7VerifyCommon.c.
  1693. //
  1694. // See below examples:
  1695. //
  1696. // 1. Without ContentInfo
  1697. // 30 82 0c da // SEQUENCE (5 element) (3294 BYTES) -- SignedData
  1698. // 02 01 01 // INTEGER 1 -- Version
  1699. // 31 0f // SET (1 element) (15 BYTES) -- DigestAlgorithmIdentifiers
  1700. // 30 0d // SEQUENCE (2 element) (13 BYTES) -- AlgorithmIdentifier
  1701. // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- algorithm
  1702. // 60 86 48 01 65 03 04 02 01 // sha256 [2.16.840.1.101.3.4.2.1]
  1703. // 05 00 // NULL (0 BYTES) -- parameters
  1704. //
  1705. // Example from: https://uefi.org/revocationlistfile
  1706. //
  1707. // 2. With ContentInfo
  1708. // 30 82 05 90 // SEQUENCE (1424 BYTES) -- ContentInfo
  1709. // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- ContentType
  1710. // 2a 86 48 86 f7 0d 01 07 02 // signedData [1.2.840.113549.1.7.2]
  1711. // a0 82 05 81 // CONTEXT-SPECIFIC CONSTRUCTED TAG 0 (1409 BYTES) -- content
  1712. // 30 82 05 7d // SEQUENCE (1405 BYTES) -- SignedData
  1713. // 02 01 01 // INTEGER 1 -- Version
  1714. // 31 0f // SET (1 element) (15 BYTES) -- DigestAlgorithmIdentifiers
  1715. // 30 0d // SEQUENCE (13 BYTES) -- AlgorithmIdentifier
  1716. // 06 09 // OBJECT-IDENTIFIER (9 BYTES) -- algorithm
  1717. // 60 86 48 01 65 03 04 02 01 // sha256 [2.16.840.1.101.3.4.2.1]
  1718. // 05 00 // NULL (0 BYTES) -- parameters
  1719. //
  1720. // Example generated with: https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#Manual_process
  1721. //
  1722. if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
  1723. if ( ( (SigDataSize >= (13 + sizeof (mSha256OidValue)))
  1724. && ( ((*(SigData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE)
  1725. || (CompareMem (SigData + 13, &mSha256OidValue, sizeof (mSha256OidValue)) != 0)))
  1726. && ( (SigDataSize >= (32 + sizeof (mSha256OidValue)))
  1727. && ( ((*(SigData + 20) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE)
  1728. || (CompareMem (SigData + 32, &mSha256OidValue, sizeof (mSha256OidValue)) != 0))))
  1729. {
  1730. return EFI_SECURITY_VIOLATION;
  1731. }
  1732. }
  1733. //
  1734. // Find out the new data payload which follows Pkcs7 SignedData directly.
  1735. //
  1736. PayloadPtr = SigData + SigDataSize;
  1737. PayloadSize = DataSize - OFFSET_OF_AUTHINFO2_CERT_DATA - (UINTN)SigDataSize;
  1738. // If the VariablePolicy engine is disabled, allow deletion of any authenticated variables.
  1739. if ((PayloadSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && !IsVariablePolicyEnabled ()) {
  1740. VerifyStatus = TRUE;
  1741. goto Exit;
  1742. }
  1743. //
  1744. // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes
  1745. // parameters of the SetVariable() call and the TimeStamp component of the
  1746. // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value
  1747. // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)
  1748. //
  1749. NewDataSize = PayloadSize + sizeof (EFI_TIME) + sizeof (UINT32) +
  1750. sizeof (EFI_GUID) + StrSize (VariableName) - sizeof (CHAR16);
  1751. //
  1752. // Here is to reuse scratch data area(at the end of volatile variable store)
  1753. // to reduce SMRAM consumption for SMM variable driver.
  1754. // The scratch buffer is enough to hold the serialized data and safe to use,
  1755. // because it is only used at here to do verification temporarily first
  1756. // and then used in UpdateVariable() for a time based auth variable set.
  1757. //
  1758. Status = mAuthVarLibContextIn->GetScratchBuffer (&NewDataSize, (VOID **)&NewData);
  1759. if (EFI_ERROR (Status)) {
  1760. return EFI_OUT_OF_RESOURCES;
  1761. }
  1762. Buffer = NewData;
  1763. Length = StrLen (VariableName) * sizeof (CHAR16);
  1764. CopyMem (Buffer, VariableName, Length);
  1765. Buffer += Length;
  1766. Length = sizeof (EFI_GUID);
  1767. CopyMem (Buffer, VendorGuid, Length);
  1768. Buffer += Length;
  1769. Length = sizeof (UINT32);
  1770. CopyMem (Buffer, &Attr, Length);
  1771. Buffer += Length;
  1772. Length = sizeof (EFI_TIME);
  1773. CopyMem (Buffer, &CertData->TimeStamp, Length);
  1774. Buffer += Length;
  1775. CopyMem (Buffer, PayloadPtr, PayloadSize);
  1776. if (AuthVarType == AuthVarTypePk) {
  1777. //
  1778. // Verify that the signature has been made with the current Platform Key (no chaining for PK).
  1779. // First, get signer's certificates from SignedData.
  1780. //
  1781. VerifyStatus = Pkcs7GetSigners (
  1782. SigData,
  1783. SigDataSize,
  1784. &SignerCerts,
  1785. &CertStackSize,
  1786. &TopLevelCert,
  1787. &TopLevelCertSize
  1788. );
  1789. if (!VerifyStatus) {
  1790. goto Exit;
  1791. }
  1792. //
  1793. // Second, get the current platform key from variable. Check whether it's identical with signer's certificates
  1794. // in SignedData. If not, return error immediately.
  1795. //
  1796. Status = AuthServiceInternalFindVariable (
  1797. EFI_PLATFORM_KEY_NAME,
  1798. &gEfiGlobalVariableGuid,
  1799. &Data,
  1800. &DataSize
  1801. );
  1802. if (EFI_ERROR (Status)) {
  1803. VerifyStatus = FALSE;
  1804. goto Exit;
  1805. }
  1806. CertList = (EFI_SIGNATURE_LIST *)Data;
  1807. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  1808. if ((TopLevelCertSize != (CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1))) ||
  1809. (CompareMem (Cert->SignatureData, TopLevelCert, TopLevelCertSize) != 0))
  1810. {
  1811. VerifyStatus = FALSE;
  1812. goto Exit;
  1813. }
  1814. //
  1815. // Verify Pkcs7 SignedData via Pkcs7Verify library.
  1816. //
  1817. VerifyStatus = Pkcs7Verify (
  1818. SigData,
  1819. SigDataSize,
  1820. TopLevelCert,
  1821. TopLevelCertSize,
  1822. NewData,
  1823. NewDataSize
  1824. );
  1825. } else if (AuthVarType == AuthVarTypeKek) {
  1826. //
  1827. // Get KEK database from variable.
  1828. //
  1829. Status = AuthServiceInternalFindVariable (
  1830. EFI_KEY_EXCHANGE_KEY_NAME,
  1831. &gEfiGlobalVariableGuid,
  1832. &Data,
  1833. &DataSize
  1834. );
  1835. if (EFI_ERROR (Status)) {
  1836. return Status;
  1837. }
  1838. //
  1839. // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.
  1840. //
  1841. KekDataSize = (UINT32)DataSize;
  1842. CertList = (EFI_SIGNATURE_LIST *)Data;
  1843. while ((KekDataSize > 0) && (KekDataSize >= CertList->SignatureListSize)) {
  1844. if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {
  1845. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  1846. CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
  1847. for (Index = 0; Index < CertCount; Index++) {
  1848. //
  1849. // Iterate each Signature Data Node within this CertList for a verify
  1850. //
  1851. TrustedCert = Cert->SignatureData;
  1852. TrustedCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);
  1853. //
  1854. // Verify Pkcs7 SignedData via Pkcs7Verify library.
  1855. //
  1856. VerifyStatus = Pkcs7Verify (
  1857. SigData,
  1858. SigDataSize,
  1859. TrustedCert,
  1860. TrustedCertSize,
  1861. NewData,
  1862. NewDataSize
  1863. );
  1864. if (VerifyStatus) {
  1865. goto Exit;
  1866. }
  1867. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)Cert + CertList->SignatureSize);
  1868. }
  1869. }
  1870. KekDataSize -= CertList->SignatureListSize;
  1871. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  1872. }
  1873. } else if (AuthVarType == AuthVarTypePriv) {
  1874. //
  1875. // Process common authenticated variable except PK/KEK/DB/DBX/DBT.
  1876. // Get signer's certificates from SignedData.
  1877. //
  1878. VerifyStatus = Pkcs7GetSigners (
  1879. SigData,
  1880. SigDataSize,
  1881. &SignerCerts,
  1882. &CertStackSize,
  1883. &TopLevelCert,
  1884. &TopLevelCertSize
  1885. );
  1886. if (!VerifyStatus) {
  1887. goto Exit;
  1888. }
  1889. //
  1890. // Get previously stored signer's certificates from certdb or certdbv for existing
  1891. // variable. Check whether they are identical with signer's certificates
  1892. // in SignedData. If not, return error immediately.
  1893. //
  1894. if (OrgTimeStamp != NULL) {
  1895. VerifyStatus = FALSE;
  1896. Status = GetCertsFromDb (VariableName, VendorGuid, Attributes, &CertsInCertDb, &CertsSizeinDb);
  1897. if (EFI_ERROR (Status)) {
  1898. goto Exit;
  1899. }
  1900. if (CertsSizeinDb == SHA256_DIGEST_SIZE) {
  1901. //
  1902. // Check hash of signer cert CommonName + Top-level issuer tbsCertificate against data in CertDb
  1903. //
  1904. CertDataPtr = (EFI_CERT_DATA *)(SignerCerts + 1);
  1905. Status = CalculatePrivAuthVarSignChainSHA256Digest (
  1906. CertDataPtr->CertDataBuffer,
  1907. ReadUnaligned32 ((UINT32 *)&(CertDataPtr->CertDataLength)),
  1908. TopLevelCert,
  1909. TopLevelCertSize,
  1910. Sha256Digest
  1911. );
  1912. if (EFI_ERROR (Status) || (CompareMem (Sha256Digest, CertsInCertDb, CertsSizeinDb) != 0)) {
  1913. goto Exit;
  1914. }
  1915. } else {
  1916. //
  1917. // Keep backward compatible with previous solution which saves whole signer certs stack in CertDb
  1918. //
  1919. if ((CertStackSize != CertsSizeinDb) ||
  1920. (CompareMem (SignerCerts, CertsInCertDb, CertsSizeinDb) != 0))
  1921. {
  1922. goto Exit;
  1923. }
  1924. }
  1925. }
  1926. VerifyStatus = Pkcs7Verify (
  1927. SigData,
  1928. SigDataSize,
  1929. TopLevelCert,
  1930. TopLevelCertSize,
  1931. NewData,
  1932. NewDataSize
  1933. );
  1934. if (!VerifyStatus) {
  1935. goto Exit;
  1936. }
  1937. if ((OrgTimeStamp == NULL) && (PayloadSize != 0)) {
  1938. //
  1939. // When adding a new common authenticated variable, always save Hash of cn of signer cert + tbsCertificate of Top-level issuer
  1940. //
  1941. CertDataPtr = (EFI_CERT_DATA *)(SignerCerts + 1);
  1942. Status = InsertCertsToDb (
  1943. VariableName,
  1944. VendorGuid,
  1945. Attributes,
  1946. CertDataPtr->CertDataBuffer,
  1947. ReadUnaligned32 ((UINT32 *)&(CertDataPtr->CertDataLength)),
  1948. TopLevelCert,
  1949. TopLevelCertSize
  1950. );
  1951. if (EFI_ERROR (Status)) {
  1952. VerifyStatus = FALSE;
  1953. goto Exit;
  1954. }
  1955. }
  1956. } else if (AuthVarType == AuthVarTypePayload) {
  1957. CertList = (EFI_SIGNATURE_LIST *)PayloadPtr;
  1958. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  1959. TrustedCert = Cert->SignatureData;
  1960. TrustedCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);
  1961. //
  1962. // Verify Pkcs7 SignedData via Pkcs7Verify library.
  1963. //
  1964. VerifyStatus = Pkcs7Verify (
  1965. SigData,
  1966. SigDataSize,
  1967. TrustedCert,
  1968. TrustedCertSize,
  1969. NewData,
  1970. NewDataSize
  1971. );
  1972. } else {
  1973. return EFI_SECURITY_VIOLATION;
  1974. }
  1975. Exit:
  1976. if ((AuthVarType == AuthVarTypePk) || (AuthVarType == AuthVarTypePriv)) {
  1977. if (TopLevelCert != NULL) {
  1978. Pkcs7FreeSigners (TopLevelCert);
  1979. }
  1980. if (SignerCerts != NULL) {
  1981. Pkcs7FreeSigners (SignerCerts);
  1982. }
  1983. }
  1984. if (!VerifyStatus) {
  1985. return EFI_SECURITY_VIOLATION;
  1986. }
  1987. Status = CheckSignatureListFormat (VariableName, VendorGuid, PayloadPtr, PayloadSize);
  1988. if (EFI_ERROR (Status)) {
  1989. return Status;
  1990. }
  1991. *VarPayloadPtr = PayloadPtr;
  1992. *VarPayloadSize = PayloadSize;
  1993. return EFI_SUCCESS;
  1994. }
  1995. /**
  1996. Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set
  1997. Caution: This function may receive untrusted input.
  1998. This function may be invoked in SMM mode, and datasize and data are external input.
  1999. This function will do basic validation, before parse the data.
  2000. This function will parse the authentication carefully to avoid security issues, like
  2001. buffer overflow, integer overflow.
  2002. @param[in] VariableName Name of Variable to be found.
  2003. @param[in] VendorGuid Variable vendor GUID.
  2004. @param[in] Data Data pointer.
  2005. @param[in] DataSize Size of Data found. If size is less than the
  2006. data, this value contains the required size.
  2007. @param[in] Attributes Attribute value of the variable.
  2008. @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload.
  2009. @param[out] VarDel Delete the variable or not.
  2010. @retval EFI_INVALID_PARAMETER Invalid parameter.
  2011. @retval EFI_SECURITY_VIOLATION The variable does NOT pass the validation
  2012. check carried out by the firmware.
  2013. @retval EFI_OUT_OF_RESOURCES Failed to process variable due to lack
  2014. of resources.
  2015. @retval EFI_SUCCESS Variable pass validation successfully.
  2016. **/
  2017. EFI_STATUS
  2018. VerifyTimeBasedPayloadAndUpdate (
  2019. IN CHAR16 *VariableName,
  2020. IN EFI_GUID *VendorGuid,
  2021. IN VOID *Data,
  2022. IN UINTN DataSize,
  2023. IN UINT32 Attributes,
  2024. IN AUTHVAR_TYPE AuthVarType,
  2025. OUT BOOLEAN *VarDel
  2026. )
  2027. {
  2028. EFI_STATUS Status;
  2029. EFI_STATUS FindStatus;
  2030. UINT8 *PayloadPtr;
  2031. UINTN PayloadSize;
  2032. EFI_VARIABLE_AUTHENTICATION_2 *CertData;
  2033. AUTH_VARIABLE_INFO OrgVariableInfo;
  2034. BOOLEAN IsDel;
  2035. ZeroMem (&OrgVariableInfo, sizeof (OrgVariableInfo));
  2036. FindStatus = mAuthVarLibContextIn->FindVariable (
  2037. VariableName,
  2038. VendorGuid,
  2039. &OrgVariableInfo
  2040. );
  2041. Status = VerifyTimeBasedPayload (
  2042. VariableName,
  2043. VendorGuid,
  2044. Data,
  2045. DataSize,
  2046. Attributes,
  2047. AuthVarType,
  2048. (!EFI_ERROR (FindStatus)) ? OrgVariableInfo.TimeStamp : NULL,
  2049. &PayloadPtr,
  2050. &PayloadSize
  2051. );
  2052. if (EFI_ERROR (Status)) {
  2053. return Status;
  2054. }
  2055. if ( !EFI_ERROR (FindStatus)
  2056. && (PayloadSize == 0)
  2057. && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0))
  2058. {
  2059. IsDel = TRUE;
  2060. } else {
  2061. IsDel = FALSE;
  2062. }
  2063. CertData = (EFI_VARIABLE_AUTHENTICATION_2 *)Data;
  2064. //
  2065. // Final step: Update/Append Variable if it pass Pkcs7Verify
  2066. //
  2067. Status = AuthServiceInternalUpdateVariableWithTimeStamp (
  2068. VariableName,
  2069. VendorGuid,
  2070. PayloadPtr,
  2071. PayloadSize,
  2072. Attributes,
  2073. &CertData->TimeStamp
  2074. );
  2075. //
  2076. // Delete signer's certificates when delete the common authenticated variable.
  2077. //
  2078. if (IsDel && (AuthVarType == AuthVarTypePriv) && !EFI_ERROR (Status)) {
  2079. Status = DeleteCertsFromDb (VariableName, VendorGuid, Attributes);
  2080. }
  2081. if (VarDel != NULL) {
  2082. if (IsDel && !EFI_ERROR (Status)) {
  2083. *VarDel = TRUE;
  2084. } else {
  2085. *VarDel = FALSE;
  2086. }
  2087. }
  2088. return Status;
  2089. }