TlsAuthConfigImpl.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /** @file
  2. The Miscellaneous Routines for TlsAuthConfigDxe driver.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "TlsAuthConfigImpl.h"
  7. VOID *mStartOpCodeHandle = NULL;
  8. VOID *mEndOpCodeHandle = NULL;
  9. EFI_IFR_GUID_LABEL *mStartLabel = NULL;
  10. EFI_IFR_GUID_LABEL *mEndLabel = NULL;
  11. CHAR16 mTlsAuthConfigStorageName[] = L"TLS_AUTH_CONFIG_IFR_NVDATA";
  12. TLS_AUTH_CONFIG_PRIVATE_DATA *mTlsAuthPrivateData = NULL;
  13. HII_VENDOR_DEVICE_PATH mTlsAuthConfigHiiVendorDevicePath = {
  14. {
  15. {
  16. HARDWARE_DEVICE_PATH,
  17. HW_VENDOR_DP,
  18. {
  19. (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
  20. (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
  21. }
  22. },
  23. TLS_AUTH_CONFIG_GUID
  24. },
  25. {
  26. END_DEVICE_PATH_TYPE,
  27. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  28. {
  29. (UINT8)(END_DEVICE_PATH_LENGTH),
  30. (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
  31. }
  32. }
  33. };
  34. //
  35. // Possible DER-encoded certificate file suffixes, end with NULL pointer.
  36. //
  37. CHAR16 *mDerPemEncodedSuffix[] = {
  38. L".cer",
  39. L".der",
  40. L".crt",
  41. L".pem",
  42. NULL
  43. };
  44. /**
  45. This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
  46. @param[in] FileSuffix The suffix of the input certificate file
  47. @retval TRUE It's a DER/PEM-encoded certificate.
  48. @retval FALSE It's NOT a DER/PEM-encoded certificate.
  49. **/
  50. BOOLEAN
  51. IsDerPemEncodeCertificate (
  52. IN CONST CHAR16 *FileSuffix
  53. )
  54. {
  55. UINTN Index;
  56. for (Index = 0; mDerPemEncodedSuffix[Index] != NULL; Index++) {
  57. if (StrCmp (FileSuffix, mDerPemEncodedSuffix[Index]) == 0) {
  58. return TRUE;
  59. }
  60. }
  61. return FALSE;
  62. }
  63. /**
  64. Worker function that prints an EFI_GUID into specified Buffer.
  65. @param[in] Guid Pointer to GUID to print.
  66. @param[in] Buffer Buffer to print Guid into.
  67. @param[in] BufferSize Size of Buffer.
  68. @retval Number of characters printed.
  69. **/
  70. UINTN
  71. GuidToString (
  72. IN EFI_GUID *Guid,
  73. IN CHAR16 *Buffer,
  74. IN UINTN BufferSize
  75. )
  76. {
  77. return UnicodeSPrint (
  78. Buffer,
  79. BufferSize,
  80. L"%g",
  81. Guid
  82. );
  83. }
  84. /**
  85. List all cert in specified database by GUID in the page
  86. for user to select and delete as needed.
  87. @param[in] PrivateData Module's private data.
  88. @param[in] VariableName The variable name of the vendor's signature database.
  89. @param[in] VendorGuid A unique identifier for the vendor.
  90. @param[in] LabelNumber Label number to insert opcodes.
  91. @param[in] FormId Form ID of current page.
  92. @param[in] QuestionIdBase Base question id of the signature list.
  93. @retval EFI_SUCCESS Success to update the signature list page
  94. @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
  95. **/
  96. EFI_STATUS
  97. UpdateDeletePage (
  98. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private,
  99. IN CHAR16 *VariableName,
  100. IN EFI_GUID *VendorGuid,
  101. IN UINT16 LabelNumber,
  102. IN EFI_FORM_ID FormId,
  103. IN EFI_QUESTION_ID QuestionIdBase
  104. )
  105. {
  106. EFI_STATUS Status;
  107. UINT32 Index;
  108. UINTN CertCount;
  109. UINTN GuidIndex;
  110. VOID *StartOpCodeHandle;
  111. VOID *EndOpCodeHandle;
  112. EFI_IFR_GUID_LABEL *StartLabel;
  113. EFI_IFR_GUID_LABEL *EndLabel;
  114. UINTN DataSize;
  115. UINT8 *Data;
  116. EFI_SIGNATURE_LIST *CertList;
  117. EFI_SIGNATURE_DATA *Cert;
  118. UINT32 ItemDataSize;
  119. CHAR16 *GuidStr;
  120. EFI_STRING_ID GuidID;
  121. EFI_STRING_ID Help;
  122. Data = NULL;
  123. CertList = NULL;
  124. Cert = NULL;
  125. GuidStr = NULL;
  126. StartOpCodeHandle = NULL;
  127. EndOpCodeHandle = NULL;
  128. //
  129. // Initialize the container for dynamic opcodes.
  130. //
  131. StartOpCodeHandle = HiiAllocateOpCodeHandle ();
  132. if (StartOpCodeHandle == NULL) {
  133. Status = EFI_OUT_OF_RESOURCES;
  134. goto ON_EXIT;
  135. }
  136. EndOpCodeHandle = HiiAllocateOpCodeHandle ();
  137. if (EndOpCodeHandle == NULL) {
  138. Status = EFI_OUT_OF_RESOURCES;
  139. goto ON_EXIT;
  140. }
  141. //
  142. // Create Hii Extend Label OpCode.
  143. //
  144. StartLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  145. StartOpCodeHandle,
  146. &gEfiIfrTianoGuid,
  147. NULL,
  148. sizeof (EFI_IFR_GUID_LABEL)
  149. );
  150. StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  151. StartLabel->Number = LabelNumber;
  152. EndLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  153. EndOpCodeHandle,
  154. &gEfiIfrTianoGuid,
  155. NULL,
  156. sizeof (EFI_IFR_GUID_LABEL)
  157. );
  158. EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  159. EndLabel->Number = LABEL_END;
  160. //
  161. // Read Variable.
  162. //
  163. DataSize = 0;
  164. Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, Data);
  165. if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
  166. goto ON_EXIT;
  167. }
  168. Data = (UINT8 *)AllocateZeroPool (DataSize);
  169. if (Data == NULL) {
  170. Status = EFI_OUT_OF_RESOURCES;
  171. goto ON_EXIT;
  172. }
  173. Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, Data);
  174. if (EFI_ERROR (Status)) {
  175. goto ON_EXIT;
  176. }
  177. GuidStr = AllocateZeroPool (100);
  178. if (GuidStr == NULL) {
  179. Status = EFI_OUT_OF_RESOURCES;
  180. goto ON_EXIT;
  181. }
  182. //
  183. // Enumerate all data.
  184. //
  185. ItemDataSize = (UINT32)DataSize;
  186. CertList = (EFI_SIGNATURE_LIST *)Data;
  187. GuidIndex = 0;
  188. while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) {
  189. if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {
  190. Help = STRING_TOKEN (STR_CERT_TYPE_PCKS_GUID);
  191. } else {
  192. //
  193. // The signature type is not supported in current implementation.
  194. //
  195. ItemDataSize -= CertList->SignatureListSize;
  196. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  197. continue;
  198. }
  199. CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
  200. for (Index = 0; Index < CertCount; Index++) {
  201. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList
  202. + sizeof (EFI_SIGNATURE_LIST)
  203. + CertList->SignatureHeaderSize
  204. + Index * CertList->SignatureSize);
  205. //
  206. // Display GUID and help
  207. //
  208. GuidToString (&Cert->SignatureOwner, GuidStr, 100);
  209. GuidID = HiiSetString (Private->RegisteredHandle, 0, GuidStr, NULL);
  210. HiiCreateCheckBoxOpCode (
  211. StartOpCodeHandle,
  212. (EFI_QUESTION_ID)(QuestionIdBase + GuidIndex++),
  213. 0,
  214. 0,
  215. GuidID,
  216. Help,
  217. EFI_IFR_FLAG_CALLBACK,
  218. 0,
  219. NULL
  220. );
  221. }
  222. ItemDataSize -= CertList->SignatureListSize;
  223. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  224. }
  225. ON_EXIT:
  226. HiiUpdateForm (
  227. Private->RegisteredHandle,
  228. &gTlsAuthConfigGuid,
  229. FormId,
  230. StartOpCodeHandle,
  231. EndOpCodeHandle
  232. );
  233. if (StartOpCodeHandle != NULL) {
  234. HiiFreeOpCodeHandle (StartOpCodeHandle);
  235. }
  236. if (EndOpCodeHandle != NULL) {
  237. HiiFreeOpCodeHandle (EndOpCodeHandle);
  238. }
  239. if (Data != NULL) {
  240. FreePool (Data);
  241. }
  242. if (GuidStr != NULL) {
  243. FreePool (GuidStr);
  244. }
  245. return EFI_SUCCESS;
  246. }
  247. /**
  248. Delete one entry from cert database.
  249. @param[in] Private Module's private data.
  250. @param[in] VariableName The variable name of the database.
  251. @param[in] VendorGuid A unique identifier for the vendor.
  252. @param[in] LabelNumber Label number to insert opcodes.
  253. @param[in] FormId Form ID of current page.
  254. @param[in] QuestionIdBase Base question id of the cert list.
  255. @param[in] DeleteIndex Cert index to delete.
  256. @retval EFI_SUCCESS Delete signature successfully.
  257. @retval EFI_NOT_FOUND Can't find the signature item,
  258. @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
  259. **/
  260. EFI_STATUS
  261. DeleteCert (
  262. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private,
  263. IN CHAR16 *VariableName,
  264. IN EFI_GUID *VendorGuid,
  265. IN UINT16 LabelNumber,
  266. IN EFI_FORM_ID FormId,
  267. IN EFI_QUESTION_ID QuestionIdBase,
  268. IN UINTN DeleteIndex
  269. )
  270. {
  271. EFI_STATUS Status;
  272. UINTN DataSize;
  273. UINT8 *Data;
  274. UINT8 *OldData;
  275. UINT32 Attr;
  276. UINT32 Index;
  277. EFI_SIGNATURE_LIST *CertList;
  278. EFI_SIGNATURE_LIST *NewCertList;
  279. EFI_SIGNATURE_DATA *Cert;
  280. UINTN CertCount;
  281. UINT32 Offset;
  282. BOOLEAN IsItemFound;
  283. UINT32 ItemDataSize;
  284. UINTN GuidIndex;
  285. Data = NULL;
  286. OldData = NULL;
  287. CertList = NULL;
  288. Cert = NULL;
  289. Attr = 0;
  290. //
  291. // Get original signature list data.
  292. //
  293. DataSize = 0;
  294. Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, NULL);
  295. if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
  296. goto ON_EXIT;
  297. }
  298. OldData = (UINT8 *)AllocateZeroPool (DataSize);
  299. if (OldData == NULL) {
  300. Status = EFI_OUT_OF_RESOURCES;
  301. goto ON_EXIT;
  302. }
  303. Status = gRT->GetVariable (VariableName, VendorGuid, &Attr, &DataSize, OldData);
  304. if (EFI_ERROR (Status)) {
  305. goto ON_EXIT;
  306. }
  307. //
  308. // Allocate space for new variable.
  309. //
  310. Data = (UINT8 *)AllocateZeroPool (DataSize);
  311. if (Data == NULL) {
  312. Status = EFI_OUT_OF_RESOURCES;
  313. goto ON_EXIT;
  314. }
  315. //
  316. // Enumerate all data and erasing the target item.
  317. //
  318. IsItemFound = FALSE;
  319. ItemDataSize = (UINT32)DataSize;
  320. CertList = (EFI_SIGNATURE_LIST *)OldData;
  321. Offset = 0;
  322. GuidIndex = 0;
  323. while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) {
  324. if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {
  325. //
  326. // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
  327. //
  328. CopyMem (Data + Offset, CertList, (sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize));
  329. NewCertList = (EFI_SIGNATURE_LIST *)(Data + Offset);
  330. Offset += (sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  331. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
  332. CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
  333. for (Index = 0; Index < CertCount; Index++) {
  334. if (GuidIndex == DeleteIndex) {
  335. //
  336. // Find it! Skip it!
  337. //
  338. NewCertList->SignatureListSize -= CertList->SignatureSize;
  339. IsItemFound = TRUE;
  340. } else {
  341. //
  342. // This item doesn't match. Copy it to the Data buffer.
  343. //
  344. CopyMem (Data + Offset, (UINT8 *)(Cert), CertList->SignatureSize);
  345. Offset += CertList->SignatureSize;
  346. }
  347. GuidIndex++;
  348. Cert = (EFI_SIGNATURE_DATA *)((UINT8 *)Cert + CertList->SignatureSize);
  349. }
  350. } else {
  351. //
  352. // This List doesn't match. Just copy it to the Data buffer.
  353. //
  354. CopyMem (Data + Offset, (UINT8 *)(CertList), CertList->SignatureListSize);
  355. Offset += CertList->SignatureListSize;
  356. }
  357. ItemDataSize -= CertList->SignatureListSize;
  358. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  359. }
  360. if (!IsItemFound) {
  361. //
  362. // Doesn't find the signature Item!
  363. //
  364. Status = EFI_NOT_FOUND;
  365. goto ON_EXIT;
  366. }
  367. //
  368. // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
  369. //
  370. ItemDataSize = Offset;
  371. CertList = (EFI_SIGNATURE_LIST *)Data;
  372. Offset = 0;
  373. ZeroMem (OldData, ItemDataSize);
  374. while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) {
  375. CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
  376. DEBUG ((DEBUG_INFO, " CertCount = %x\n", CertCount));
  377. if (CertCount != 0) {
  378. CopyMem (OldData + Offset, (UINT8 *)(CertList), CertList->SignatureListSize);
  379. Offset += CertList->SignatureListSize;
  380. }
  381. ItemDataSize -= CertList->SignatureListSize;
  382. CertList = (EFI_SIGNATURE_LIST *)((UINT8 *)CertList + CertList->SignatureListSize);
  383. }
  384. DataSize = Offset;
  385. Status = gRT->SetVariable (
  386. VariableName,
  387. VendorGuid,
  388. Attr,
  389. DataSize,
  390. OldData
  391. );
  392. if (EFI_ERROR (Status)) {
  393. DEBUG ((DEBUG_ERROR, "Failed to set variable, Status = %r\n", Status));
  394. goto ON_EXIT;
  395. }
  396. ON_EXIT:
  397. if (Data != NULL) {
  398. FreePool (Data);
  399. }
  400. if (OldData != NULL) {
  401. FreePool (OldData);
  402. }
  403. return UpdateDeletePage (
  404. Private,
  405. VariableName,
  406. VendorGuid,
  407. LabelNumber,
  408. FormId,
  409. QuestionIdBase
  410. );
  411. }
  412. /**
  413. Clean the file related resource.
  414. @param[in] Private Module's private data.
  415. **/
  416. VOID
  417. CleanFileContext (
  418. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private
  419. )
  420. {
  421. if (Private->FileContext->FHandle != NULL) {
  422. Private->FileContext->FHandle->Close (Private->FileContext->FHandle);
  423. Private->FileContext->FHandle = NULL;
  424. if (Private->FileContext->FileName != NULL) {
  425. FreePool (Private->FileContext->FileName);
  426. Private->FileContext->FileName = NULL;
  427. }
  428. }
  429. }
  430. /**
  431. Read file content into BufferPtr, the size of the allocate buffer
  432. is *FileSize plus AddtionAllocateSize.
  433. @param[in] FileHandle The file to be read.
  434. @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
  435. @param[out] FileSize Size of input file
  436. @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
  437. In case the buffer need to contain others besides the file content.
  438. @retval EFI_SUCCESS The file was read into the buffer.
  439. @retval EFI_INVALID_PARAMETER A parameter was invalid.
  440. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  441. @retval others Unexpected error.
  442. **/
  443. EFI_STATUS
  444. ReadFileContent (
  445. IN EFI_FILE_HANDLE FileHandle,
  446. IN OUT VOID **BufferPtr,
  447. OUT UINTN *FileSize,
  448. IN UINTN AddtionAllocateSize
  449. )
  450. {
  451. UINTN BufferSize;
  452. UINT64 SourceFileSize;
  453. VOID *Buffer;
  454. EFI_STATUS Status;
  455. if ((FileHandle == NULL) || (FileSize == NULL)) {
  456. return EFI_INVALID_PARAMETER;
  457. }
  458. Buffer = NULL;
  459. //
  460. // Get the file size
  461. //
  462. Status = FileHandle->SetPosition (FileHandle, (UINT64)-1);
  463. if (EFI_ERROR (Status)) {
  464. goto ON_EXIT;
  465. }
  466. Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);
  467. if (EFI_ERROR (Status)) {
  468. goto ON_EXIT;
  469. }
  470. Status = FileHandle->SetPosition (FileHandle, 0);
  471. if (EFI_ERROR (Status)) {
  472. goto ON_EXIT;
  473. }
  474. BufferSize = (UINTN)SourceFileSize + AddtionAllocateSize;
  475. Buffer = AllocateZeroPool (BufferSize);
  476. if (Buffer == NULL) {
  477. return EFI_OUT_OF_RESOURCES;
  478. }
  479. BufferSize = (UINTN)SourceFileSize;
  480. *FileSize = BufferSize;
  481. Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);
  482. if (EFI_ERROR (Status) || (BufferSize != *FileSize)) {
  483. FreePool (Buffer);
  484. Buffer = NULL;
  485. Status = EFI_BAD_BUFFER_SIZE;
  486. goto ON_EXIT;
  487. }
  488. ON_EXIT:
  489. *BufferPtr = Buffer;
  490. return Status;
  491. }
  492. /**
  493. This function converts an input device structure to a Unicode string.
  494. @param[in] DevPath A pointer to the device path structure.
  495. @return A new allocated Unicode string that represents the device path.
  496. **/
  497. CHAR16 *
  498. EFIAPI
  499. DevicePathToStr (
  500. IN EFI_DEVICE_PATH_PROTOCOL *DevPath
  501. )
  502. {
  503. return ConvertDevicePathToText (
  504. DevPath,
  505. FALSE,
  506. TRUE
  507. );
  508. }
  509. /**
  510. Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
  511. The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
  512. means not enough memory resource.
  513. @param DevicePath Device path.
  514. @retval NULL Not enough memory resource for AllocateCopyPool.
  515. @retval Other A new allocated string that represents the file name.
  516. **/
  517. CHAR16 *
  518. ExtractFileNameFromDevicePath (
  519. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
  520. )
  521. {
  522. CHAR16 *String;
  523. CHAR16 *MatchString;
  524. CHAR16 *LastMatch;
  525. CHAR16 *FileName;
  526. UINTN Length;
  527. ASSERT (DevicePath != NULL);
  528. String = DevicePathToStr (DevicePath);
  529. MatchString = String;
  530. LastMatch = String;
  531. FileName = NULL;
  532. while (MatchString != NULL) {
  533. LastMatch = MatchString + 1;
  534. MatchString = StrStr (LastMatch, L"\\");
  535. }
  536. Length = StrLen (LastMatch);
  537. FileName = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), LastMatch);
  538. if (FileName != NULL) {
  539. *(FileName + Length) = 0;
  540. }
  541. FreePool (String);
  542. return FileName;
  543. }
  544. /**
  545. Enroll a new X509 certificate into Variable.
  546. @param[in] PrivateData The module's private data.
  547. @param[in] VariableName Variable name of CA database.
  548. @retval EFI_SUCCESS New X509 is enrolled successfully.
  549. @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
  550. **/
  551. EFI_STATUS
  552. EnrollX509toVariable (
  553. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private,
  554. IN CHAR16 *VariableName
  555. )
  556. {
  557. EFI_STATUS Status;
  558. UINTN X509DataSize;
  559. VOID *X509Data;
  560. EFI_SIGNATURE_LIST *CACert;
  561. EFI_SIGNATURE_DATA *CACertData;
  562. VOID *Data;
  563. UINTN DataSize;
  564. UINTN SigDataSize;
  565. UINT32 Attr;
  566. X509DataSize = 0;
  567. SigDataSize = 0;
  568. DataSize = 0;
  569. X509Data = NULL;
  570. CACert = NULL;
  571. CACertData = NULL;
  572. Data = NULL;
  573. Attr = 0;
  574. Status = ReadFileContent (
  575. Private->FileContext->FHandle,
  576. &X509Data,
  577. &X509DataSize,
  578. 0
  579. );
  580. if (EFI_ERROR (Status)) {
  581. goto ON_EXIT;
  582. }
  583. ASSERT (X509Data != NULL);
  584. SigDataSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + X509DataSize;
  585. Data = AllocateZeroPool (SigDataSize);
  586. if (Data == NULL) {
  587. Status = EFI_OUT_OF_RESOURCES;
  588. goto ON_EXIT;
  589. }
  590. //
  591. // Fill Certificate Database parameters.
  592. //
  593. CACert = (EFI_SIGNATURE_LIST *)Data;
  594. CACert->SignatureListSize = (UINT32)SigDataSize;
  595. CACert->SignatureHeaderSize = 0;
  596. CACert->SignatureSize = (UINT32)(sizeof (EFI_SIGNATURE_DATA) - 1 + X509DataSize);
  597. CopyGuid (&CACert->SignatureType, &gEfiCertX509Guid);
  598. CACertData = (EFI_SIGNATURE_DATA *)((UINT8 *)CACert + sizeof (EFI_SIGNATURE_LIST));
  599. CopyGuid (&CACertData->SignatureOwner, Private->CertGuid);
  600. CopyMem ((UINT8 *)(CACertData->SignatureData), X509Data, X509DataSize);
  601. //
  602. // Check if the signature database entry already exists. If it does, use the
  603. // EFI_VARIABLE_APPEND_WRITE attribute to append the new signature data to
  604. // the original variable, plus preserve the original variable attributes.
  605. //
  606. Status = gRT->GetVariable (
  607. VariableName,
  608. &gEfiTlsCaCertificateGuid,
  609. &Attr,
  610. &DataSize,
  611. NULL
  612. );
  613. if (Status == EFI_BUFFER_TOO_SMALL) {
  614. Attr |= EFI_VARIABLE_APPEND_WRITE;
  615. } else if (Status == EFI_NOT_FOUND) {
  616. Attr = TLS_AUTH_CONFIG_VAR_BASE_ATTR;
  617. } else {
  618. goto ON_EXIT;
  619. }
  620. Status = gRT->SetVariable (
  621. VariableName,
  622. &gEfiTlsCaCertificateGuid,
  623. Attr,
  624. SigDataSize,
  625. Data
  626. );
  627. if (EFI_ERROR (Status)) {
  628. goto ON_EXIT;
  629. }
  630. ON_EXIT:
  631. CleanFileContext (Private);
  632. if (Private->CertGuid != NULL) {
  633. FreePool (Private->CertGuid);
  634. Private->CertGuid = NULL;
  635. }
  636. if (Data != NULL) {
  637. FreePool (Data);
  638. }
  639. if (X509Data != NULL) {
  640. FreePool (X509Data);
  641. }
  642. return Status;
  643. }
  644. /**
  645. Enroll Cert into TlsCaCertificate. The GUID will be Private->CertGuid.
  646. @param[in] PrivateData The module's private data.
  647. @param[in] VariableName Variable name of signature database.
  648. @retval EFI_SUCCESS New Cert enrolled successfully.
  649. @retval EFI_INVALID_PARAMETER The parameter is invalid.
  650. @retval EFI_UNSUPPORTED The Cert file is unsupported type.
  651. @retval others Fail to enroll Cert data.
  652. **/
  653. EFI_STATUS
  654. EnrollCertDatabase (
  655. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private,
  656. IN CHAR16 *VariableName
  657. )
  658. {
  659. UINT16 *FilePostFix;
  660. UINTN NameLength;
  661. if ((Private->FileContext->FileName == NULL) || (Private->FileContext->FHandle == NULL) || (Private->CertGuid == NULL)) {
  662. return EFI_INVALID_PARAMETER;
  663. }
  664. //
  665. // Parse the file's postfix.
  666. //
  667. NameLength = StrLen (Private->FileContext->FileName);
  668. if (NameLength <= 4) {
  669. return EFI_INVALID_PARAMETER;
  670. }
  671. FilePostFix = Private->FileContext->FileName + NameLength - 4;
  672. if (IsDerPemEncodeCertificate (FilePostFix)) {
  673. //
  674. // Supports DER-encoded X509 certificate.
  675. //
  676. return EnrollX509toVariable (Private, VariableName);
  677. }
  678. return EFI_UNSUPPORTED;
  679. }
  680. /**
  681. Refresh the global UpdateData structure.
  682. **/
  683. VOID
  684. RefreshUpdateData (
  685. VOID
  686. )
  687. {
  688. //
  689. // Free current updated date
  690. //
  691. if (mStartOpCodeHandle != NULL) {
  692. HiiFreeOpCodeHandle (mStartOpCodeHandle);
  693. }
  694. //
  695. // Create new OpCode Handle
  696. //
  697. mStartOpCodeHandle = HiiAllocateOpCodeHandle ();
  698. //
  699. // Create Hii Extend Label OpCode as the start opcode
  700. //
  701. mStartLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  702. mStartOpCodeHandle,
  703. &gEfiIfrTianoGuid,
  704. NULL,
  705. sizeof (EFI_IFR_GUID_LABEL)
  706. );
  707. mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  708. }
  709. /**
  710. Clean up the dynamic opcode at label and form specified by both LabelId.
  711. @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.
  712. @param[in] PrivateData Module private data.
  713. **/
  714. VOID
  715. CleanUpPage (
  716. IN UINT16 LabelId,
  717. IN TLS_AUTH_CONFIG_PRIVATE_DATA *PrivateData
  718. )
  719. {
  720. RefreshUpdateData ();
  721. //
  722. // Remove all op-codes from dynamic page
  723. //
  724. mStartLabel->Number = LabelId;
  725. HiiUpdateForm (
  726. PrivateData->RegisteredHandle,
  727. &gTlsAuthConfigGuid,
  728. LabelId,
  729. mStartOpCodeHandle, // Label LabelId
  730. mEndOpCodeHandle // LABEL_END
  731. );
  732. }
  733. /**
  734. Update the form base on the selected file.
  735. @param FilePath Point to the file path.
  736. @param FormId The form need to display.
  737. @retval TRUE Exit caller function.
  738. @retval FALSE Not exit caller function.
  739. **/
  740. BOOLEAN
  741. UpdatePage (
  742. IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
  743. IN EFI_FORM_ID FormId
  744. )
  745. {
  746. CHAR16 *FileName;
  747. EFI_STRING_ID StringToken;
  748. FileName = NULL;
  749. if (FilePath != NULL) {
  750. FileName = ExtractFileNameFromDevicePath (FilePath);
  751. }
  752. if (FileName == NULL) {
  753. //
  754. // FileName = NULL has two case:
  755. // 1. FilePath == NULL, not select file.
  756. // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
  757. // In these two case, no need to update the form, and exit the caller function.
  758. //
  759. return TRUE;
  760. }
  761. StringToken = HiiSetString (mTlsAuthPrivateData->RegisteredHandle, 0, FileName, NULL);
  762. mTlsAuthPrivateData->FileContext->FileName = FileName;
  763. EfiOpenFileByDevicePath (
  764. &FilePath,
  765. &mTlsAuthPrivateData->FileContext->FHandle,
  766. EFI_FILE_MODE_READ,
  767. 0
  768. );
  769. //
  770. // Create Subtitle op-code for the display string of the option.
  771. //
  772. RefreshUpdateData ();
  773. mStartLabel->Number = FormId;
  774. HiiCreateSubTitleOpCode (
  775. mStartOpCodeHandle,
  776. StringToken,
  777. 0,
  778. 0,
  779. 0
  780. );
  781. HiiUpdateForm (
  782. mTlsAuthPrivateData->RegisteredHandle,
  783. &gTlsAuthConfigGuid,
  784. FormId,
  785. mStartOpCodeHandle, /// Label FormId
  786. mEndOpCodeHandle /// LABEL_END
  787. );
  788. return TRUE;
  789. }
  790. /**
  791. Update the form base on the input file path info.
  792. @param FilePath Point to the file path.
  793. @retval TRUE Exit caller function.
  794. @retval FALSE Not exit caller function.
  795. **/
  796. BOOLEAN
  797. EFIAPI
  798. UpdateCAFromFile (
  799. IN EFI_DEVICE_PATH_PROTOCOL *FilePath
  800. )
  801. {
  802. return UpdatePage (FilePath, TLS_AUTH_CONFIG_FORMID4_FORM);
  803. }
  804. /**
  805. Unload the configuration form, this includes: delete all the configuration
  806. entries, uninstall the form callback protocol, and free the resources used.
  807. @param[in] Private Pointer to the driver private data.
  808. @retval EFI_SUCCESS The configuration form is unloaded.
  809. @retval Others Failed to unload the form.
  810. **/
  811. EFI_STATUS
  812. TlsAuthConfigFormUnload (
  813. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private
  814. )
  815. {
  816. if (Private->DriverHandle != NULL) {
  817. //
  818. // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
  819. //
  820. gBS->UninstallMultipleProtocolInterfaces (
  821. Private->DriverHandle,
  822. &gEfiDevicePathProtocolGuid,
  823. &mTlsAuthConfigHiiVendorDevicePath,
  824. &gEfiHiiConfigAccessProtocolGuid,
  825. &Private->ConfigAccess,
  826. NULL
  827. );
  828. Private->DriverHandle = NULL;
  829. }
  830. if (Private->RegisteredHandle != NULL) {
  831. //
  832. // Remove HII package list
  833. //
  834. HiiRemovePackages (Private->RegisteredHandle);
  835. Private->RegisteredHandle = NULL;
  836. }
  837. if (Private->CertGuid != NULL) {
  838. FreePool (Private->CertGuid);
  839. }
  840. if (Private->FileContext != NULL) {
  841. FreePool (Private->FileContext);
  842. }
  843. FreePool (Private);
  844. if (mStartOpCodeHandle != NULL) {
  845. HiiFreeOpCodeHandle (mStartOpCodeHandle);
  846. }
  847. if (mEndOpCodeHandle != NULL) {
  848. HiiFreeOpCodeHandle (mEndOpCodeHandle);
  849. }
  850. return EFI_SUCCESS;
  851. }
  852. /**
  853. Initialize the configuration form.
  854. @param[in] Private Pointer to the driver private data.
  855. @retval EFI_SUCCESS The configuration form is initialized.
  856. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  857. **/
  858. EFI_STATUS
  859. TlsAuthConfigFormInit (
  860. IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private
  861. )
  862. {
  863. EFI_STATUS Status;
  864. Private->Signature = TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE;
  865. Private->ConfigAccess.ExtractConfig = TlsAuthConfigAccessExtractConfig;
  866. Private->ConfigAccess.RouteConfig = TlsAuthConfigAccessRouteConfig;
  867. Private->ConfigAccess.Callback = TlsAuthConfigAccessCallback;
  868. //
  869. // Install Device Path Protocol and Config Access protocol to driver handle.
  870. //
  871. Status = gBS->InstallMultipleProtocolInterfaces (
  872. &Private->DriverHandle,
  873. &gEfiDevicePathProtocolGuid,
  874. &mTlsAuthConfigHiiVendorDevicePath,
  875. &gEfiHiiConfigAccessProtocolGuid,
  876. &Private->ConfigAccess,
  877. NULL
  878. );
  879. if (EFI_ERROR (Status)) {
  880. return Status;
  881. }
  882. //
  883. // Publish our HII data.
  884. //
  885. Private->RegisteredHandle = HiiAddPackages (
  886. &gTlsAuthConfigGuid,
  887. Private->DriverHandle,
  888. TlsAuthConfigDxeStrings,
  889. TlsAuthConfigVfrBin,
  890. NULL
  891. );
  892. if (Private->RegisteredHandle == NULL) {
  893. Status = EFI_OUT_OF_RESOURCES;
  894. goto Error;
  895. }
  896. Private->FileContext = AllocateZeroPool (sizeof (TLS_AUTH_CONFIG_FILE_CONTEXT));
  897. if (Private->FileContext == NULL) {
  898. Status = EFI_OUT_OF_RESOURCES;
  899. goto Error;
  900. }
  901. //
  902. // Init OpCode Handle and Allocate space for creation of Buffer
  903. //
  904. mStartOpCodeHandle = HiiAllocateOpCodeHandle ();
  905. if (mStartOpCodeHandle == NULL) {
  906. Status = EFI_OUT_OF_RESOURCES;
  907. goto Error;
  908. }
  909. mEndOpCodeHandle = HiiAllocateOpCodeHandle ();
  910. if (mEndOpCodeHandle == NULL) {
  911. Status = EFI_OUT_OF_RESOURCES;
  912. goto Error;
  913. }
  914. //
  915. // Create Hii Extend Label OpCode as the start opcode
  916. //
  917. mStartLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  918. mStartOpCodeHandle,
  919. &gEfiIfrTianoGuid,
  920. NULL,
  921. sizeof (EFI_IFR_GUID_LABEL)
  922. );
  923. mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  924. //
  925. // Create Hii Extend Label OpCode as the end opcode
  926. //
  927. mEndLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  928. mEndOpCodeHandle,
  929. &gEfiIfrTianoGuid,
  930. NULL,
  931. sizeof (EFI_IFR_GUID_LABEL)
  932. );
  933. mEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  934. mEndLabel->Number = LABEL_END;
  935. return EFI_SUCCESS;
  936. Error:
  937. TlsAuthConfigFormUnload (Private);
  938. return Status;
  939. }
  940. /**
  941. This function allows the caller to request the current
  942. configuration for one or more named elements. The resulting
  943. string is in <ConfigAltResp> format. Any and all alternative
  944. configuration strings shall also be appended to the end of the
  945. current configuration string. If they are, they must appear
  946. after the current configuration. They must contain the same
  947. routing (GUID, NAME, PATH) as the current configuration string.
  948. They must have an additional description indicating the type of
  949. alternative configuration the string represents,
  950. "ALTCFG=<StringToken>". That <StringToken> (when
  951. converted from Hex UNICODE to binary) is a reference to a
  952. string in the associated string pack.
  953. @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  954. @param Request A null-terminated Unicode string in
  955. <ConfigRequest> format. Note that this
  956. includes the routing information as well as
  957. the configurable name / value pairs. It is
  958. invalid for this string to be in
  959. <MultiConfigRequest> format.
  960. If a NULL is passed in for the Request field,
  961. all of the settings being abstracted by this function
  962. will be returned in the Results field. In addition,
  963. if a ConfigHdr is passed in with no request elements,
  964. all of the settings being abstracted for that particular
  965. ConfigHdr reference will be returned in the Results Field.
  966. @param Progress On return, points to a character in the
  967. Request string. Points to the string's null
  968. terminator if request was successful. Points
  969. to the most recent "&" before the first
  970. failing name / value pair (or the beginning
  971. of the string if the failure is in the first
  972. name / value pair) if the request was not
  973. successful.
  974. @param Results A null-terminated Unicode string in
  975. <MultiConfigAltResp> format which has all values
  976. filled in for the names in the Request string.
  977. String to be allocated by the called function.
  978. @retval EFI_SUCCESS The Results string is filled with the
  979. values corresponding to all requested
  980. names.
  981. @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
  982. parts of the results that must be
  983. stored awaiting possible future
  984. protocols.
  985. @retval EFI_NOT_FOUND Routing data doesn't match any
  986. known driver. Progress set to the
  987. first character in the routing header.
  988. Note: There is no requirement that the
  989. driver validate the routing data. It
  990. must skip the <ConfigHdr> in order to
  991. process the names.
  992. @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
  993. to most recent "&" before the
  994. error or the beginning of the
  995. string.
  996. @retval EFI_INVALID_PARAMETER Unknown name. Progress points
  997. to the & before the name in
  998. question.
  999. **/
  1000. EFI_STATUS
  1001. EFIAPI
  1002. TlsAuthConfigAccessExtractConfig (
  1003. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  1004. IN CONST EFI_STRING Request,
  1005. OUT EFI_STRING *Progress,
  1006. OUT EFI_STRING *Results
  1007. )
  1008. {
  1009. EFI_STATUS Status;
  1010. UINTN BufferSize;
  1011. UINTN Size;
  1012. EFI_STRING ConfigRequest;
  1013. EFI_STRING ConfigRequestHdr;
  1014. TLS_AUTH_CONFIG_PRIVATE_DATA *Private;
  1015. BOOLEAN AllocatedRequest;
  1016. if ((Progress == NULL) || (Results == NULL)) {
  1017. return EFI_INVALID_PARAMETER;
  1018. }
  1019. AllocatedRequest = FALSE;
  1020. ConfigRequestHdr = NULL;
  1021. ConfigRequest = NULL;
  1022. Size = 0;
  1023. Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This);
  1024. BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA);
  1025. ZeroMem (&Private->TlsAuthConfigNvData, BufferSize);
  1026. *Progress = Request;
  1027. if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gTlsAuthConfigGuid, mTlsAuthConfigStorageName)) {
  1028. return EFI_NOT_FOUND;
  1029. }
  1030. ConfigRequest = Request;
  1031. if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
  1032. //
  1033. // Request is set to NULL or OFFSET is NULL, construct full request string.
  1034. //
  1035. // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
  1036. // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
  1037. //
  1038. ConfigRequestHdr = HiiConstructConfigHdr (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, Private->DriverHandle);
  1039. Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
  1040. ConfigRequest = AllocateZeroPool (Size);
  1041. ASSERT (ConfigRequest != NULL);
  1042. AllocatedRequest = TRUE;
  1043. UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
  1044. FreePool (ConfigRequestHdr);
  1045. ConfigRequestHdr = NULL;
  1046. }
  1047. Status = gHiiConfigRouting->BlockToConfig (
  1048. gHiiConfigRouting,
  1049. ConfigRequest,
  1050. (UINT8 *)&Private->TlsAuthConfigNvData,
  1051. BufferSize,
  1052. Results,
  1053. Progress
  1054. );
  1055. //
  1056. // Free the allocated config request string.
  1057. //
  1058. if (AllocatedRequest) {
  1059. FreePool (ConfigRequest);
  1060. }
  1061. //
  1062. // Set Progress string to the original request string.
  1063. //
  1064. if (Request == NULL) {
  1065. *Progress = NULL;
  1066. } else if (StrStr (Request, L"OFFSET") == NULL) {
  1067. *Progress = Request + StrLen (Request);
  1068. }
  1069. return Status;
  1070. }
  1071. /**
  1072. This function applies changes in a driver's configuration.
  1073. Input is a Configuration, which has the routing data for this
  1074. driver followed by name / value configuration pairs. The driver
  1075. must apply those pairs to its configurable storage. If the
  1076. driver's configuration is stored in a linear block of data
  1077. and the driver's name / value pairs are in <BlockConfig>
  1078. format, it may use the ConfigToBlock helper function (above) to
  1079. simplify the job.
  1080. @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  1081. @param Configuration A null-terminated Unicode string in
  1082. <ConfigString> format.
  1083. @param Progress A pointer to a string filled in with the
  1084. offset of the most recent '&' before the
  1085. first failing name / value pair (or the
  1086. beginning of the string if the failure
  1087. is in the first name / value pair) or
  1088. the terminating NULL if all was
  1089. successful.
  1090. @retval EFI_SUCCESS The results have been distributed or are
  1091. awaiting distribution.
  1092. @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
  1093. parts of the results that must be
  1094. stored awaiting possible future
  1095. protocols.
  1096. @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
  1097. Results parameter would result
  1098. in this type of error.
  1099. @retval EFI_NOT_FOUND Target for the specified routing data
  1100. was not found
  1101. **/
  1102. EFI_STATUS
  1103. EFIAPI
  1104. TlsAuthConfigAccessRouteConfig (
  1105. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  1106. IN CONST EFI_STRING Configuration,
  1107. OUT EFI_STRING *Progress
  1108. )
  1109. {
  1110. EFI_STATUS Status;
  1111. UINTN BufferSize;
  1112. TLS_AUTH_CONFIG_PRIVATE_DATA *Private;
  1113. if (Progress == NULL) {
  1114. return EFI_INVALID_PARAMETER;
  1115. }
  1116. *Progress = Configuration;
  1117. if (Configuration == NULL) {
  1118. return EFI_INVALID_PARAMETER;
  1119. }
  1120. //
  1121. // Check routing data in <ConfigHdr>.
  1122. // Note: there is no name for Name/Value storage, only GUID will be checked
  1123. //
  1124. if (!HiiIsConfigHdrMatch (Configuration, &gTlsAuthConfigGuid, mTlsAuthConfigStorageName)) {
  1125. return EFI_NOT_FOUND;
  1126. }
  1127. Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This);
  1128. BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA);
  1129. ZeroMem (&Private->TlsAuthConfigNvData, BufferSize);
  1130. Status = gHiiConfigRouting->ConfigToBlock (
  1131. gHiiConfigRouting,
  1132. Configuration,
  1133. (UINT8 *)&Private->TlsAuthConfigNvData,
  1134. &BufferSize,
  1135. Progress
  1136. );
  1137. if (EFI_ERROR (Status)) {
  1138. return Status;
  1139. }
  1140. return Status;
  1141. }
  1142. /**
  1143. This function is called to provide results data to the driver.
  1144. This data consists of a unique key that is used to identify
  1145. which data is either being passed back or being asked for.
  1146. @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  1147. @param Action Specifies the type of action taken by the browser.
  1148. @param QuestionId A unique value which is sent to the original
  1149. exporting driver so that it can identify the type
  1150. of data to expect. The format of the data tends to
  1151. vary based on the opcode that generated the callback.
  1152. @param Type The type of value for the question.
  1153. @param Value A pointer to the data being sent to the original
  1154. exporting driver.
  1155. @param ActionRequest On return, points to the action requested by the
  1156. callback function.
  1157. @retval EFI_SUCCESS The callback successfully handled the action.
  1158. @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
  1159. variable and its data.
  1160. @retval EFI_DEVICE_ERROR The variable could not be saved.
  1161. @retval EFI_UNSUPPORTED The specified Action is not supported by the
  1162. callback.
  1163. **/
  1164. EFI_STATUS
  1165. EFIAPI
  1166. TlsAuthConfigAccessCallback (
  1167. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  1168. IN EFI_BROWSER_ACTION Action,
  1169. IN EFI_QUESTION_ID QuestionId,
  1170. IN UINT8 Type,
  1171. IN OUT EFI_IFR_TYPE_VALUE *Value,
  1172. OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
  1173. )
  1174. {
  1175. EFI_STATUS Status;
  1176. RETURN_STATUS RStatus;
  1177. TLS_AUTH_CONFIG_PRIVATE_DATA *Private;
  1178. UINTN BufferSize;
  1179. TLS_AUTH_CONFIG_IFR_NVDATA *IfrNvData;
  1180. UINT16 LabelId;
  1181. EFI_DEVICE_PATH_PROTOCOL *File;
  1182. EFI_HII_POPUP_PROTOCOL *HiiPopUp;
  1183. EFI_HII_POPUP_SELECTION PopUpSelect;
  1184. Status = EFI_SUCCESS;
  1185. File = NULL;
  1186. if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {
  1187. return EFI_INVALID_PARAMETER;
  1188. }
  1189. Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This);
  1190. mTlsAuthPrivateData = Private;
  1191. Status = gBS->LocateProtocol (&gEfiHiiPopupProtocolGuid, NULL, (VOID **)&HiiPopUp);
  1192. if (EFI_ERROR (Status)) {
  1193. DEBUG ((DEBUG_ERROR, "Can't find Form PopUp protocol. Exit (%r)\n", Status));
  1194. return Status;
  1195. }
  1196. //
  1197. // Retrieve uncommitted data from Browser
  1198. //
  1199. BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA);
  1200. IfrNvData = AllocateZeroPool (BufferSize);
  1201. if (IfrNvData == NULL) {
  1202. return EFI_OUT_OF_RESOURCES;
  1203. }
  1204. HiiGetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *)IfrNvData);
  1205. if ((Action != EFI_BROWSER_ACTION_CHANGED) &&
  1206. (Action != EFI_BROWSER_ACTION_CHANGING) &&
  1207. (Action != EFI_BROWSER_ACTION_FORM_CLOSE))
  1208. {
  1209. Status = EFI_UNSUPPORTED;
  1210. goto EXIT;
  1211. }
  1212. if (Action == EFI_BROWSER_ACTION_CHANGING) {
  1213. switch (QuestionId) {
  1214. case KEY_TLS_AUTH_CONFIG_CLIENT_CERT:
  1215. case KEY_TLS_AUTH_CONFIG_SERVER_CA:
  1216. //
  1217. // Clear Cert GUID.
  1218. //
  1219. ZeroMem (IfrNvData->CertGuid, sizeof (IfrNvData->CertGuid));
  1220. if (Private->CertGuid == NULL) {
  1221. Private->CertGuid = (EFI_GUID *)AllocateZeroPool (sizeof (EFI_GUID));
  1222. if (Private->CertGuid == NULL) {
  1223. return EFI_OUT_OF_RESOURCES;
  1224. }
  1225. }
  1226. if (QuestionId == KEY_TLS_AUTH_CONFIG_CLIENT_CERT) {
  1227. LabelId = TLS_AUTH_CONFIG_FORMID3_FORM;
  1228. } else {
  1229. LabelId = TLS_AUTH_CONFIG_FORMID4_FORM;
  1230. }
  1231. //
  1232. // Refresh selected file.
  1233. //
  1234. CleanUpPage (LabelId, Private);
  1235. break;
  1236. case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE:
  1237. //
  1238. // If the file is already opened, clean the file related resource first.
  1239. //
  1240. CleanFileContext (Private);
  1241. ChooseFile (NULL, NULL, UpdateCAFromFile, &File);
  1242. break;
  1243. case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT:
  1244. Status = EnrollCertDatabase (Private, EFI_TLS_CA_CERTIFICATE_VARIABLE);
  1245. if (EFI_ERROR (Status)) {
  1246. CleanFileContext (Private);
  1247. HiiPopUp->CreatePopup (
  1248. HiiPopUp,
  1249. EfiHiiPopupStyleError,
  1250. EfiHiiPopupTypeOk,
  1251. Private->RegisteredHandle,
  1252. STRING_TOKEN (STR_TLS_AUTH_ENROLL_CERT_FAILURE),
  1253. &PopUpSelect
  1254. );
  1255. }
  1256. break;
  1257. case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT:
  1258. CleanFileContext (Private);
  1259. if (Private->CertGuid != NULL) {
  1260. FreePool (Private->CertGuid);
  1261. Private->CertGuid = NULL;
  1262. }
  1263. break;
  1264. case KEY_TLS_AUTH_CONFIG_DELETE_CERT:
  1265. UpdateDeletePage (
  1266. Private,
  1267. EFI_TLS_CA_CERTIFICATE_VARIABLE,
  1268. &gEfiTlsCaCertificateGuid,
  1269. LABEL_CA_DELETE,
  1270. TLS_AUTH_CONFIG_FORMID5_FORM,
  1271. OPTION_DEL_CA_ESTION_ID
  1272. );
  1273. break;
  1274. default:
  1275. if ((QuestionId >= OPTION_DEL_CA_ESTION_ID) &&
  1276. (QuestionId < (OPTION_DEL_CA_ESTION_ID + OPTION_CONFIG_RANGE)))
  1277. {
  1278. DeleteCert (
  1279. Private,
  1280. EFI_TLS_CA_CERTIFICATE_VARIABLE,
  1281. &gEfiTlsCaCertificateGuid,
  1282. LABEL_CA_DELETE,
  1283. TLS_AUTH_CONFIG_FORMID5_FORM,
  1284. OPTION_DEL_CA_ESTION_ID,
  1285. QuestionId - OPTION_DEL_CA_ESTION_ID
  1286. );
  1287. }
  1288. break;
  1289. }
  1290. } else if (Action == EFI_BROWSER_ACTION_CHANGED) {
  1291. switch (QuestionId) {
  1292. case KEY_TLS_AUTH_CONFIG_CERT_GUID:
  1293. ASSERT (Private->CertGuid != NULL);
  1294. RStatus = StrToGuid (
  1295. IfrNvData->CertGuid,
  1296. Private->CertGuid
  1297. );
  1298. if (RETURN_ERROR (RStatus) || (IfrNvData->CertGuid[GUID_STRING_LENGTH] != L'\0')) {
  1299. Status = EFI_INVALID_PARAMETER;
  1300. break;
  1301. }
  1302. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {
  1308. CleanFileContext (Private);
  1309. }
  1310. EXIT:
  1311. if (!EFI_ERROR (Status)) {
  1312. BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA);
  1313. HiiSetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *)IfrNvData, NULL);
  1314. }
  1315. FreePool (IfrNvData);
  1316. if (File != NULL) {
  1317. FreePool (File);
  1318. File = NULL;
  1319. }
  1320. return EFI_SUCCESS;
  1321. }