WifiConnectionMgrFileUtil.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /** @file
  2. The file operation functions for WiFi Connection Manager.
  3. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "WifiConnectionMgrFileUtil.h"
  7. CHAR16 *mDerPemEncodedSuffix[] = {
  8. L".cer",
  9. L".der",
  10. L".crt",
  11. L".pem",
  12. NULL
  13. };
  14. /**
  15. This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
  16. @param[in] FileSuffix The suffix of the input certificate file
  17. @retval TRUE It's a DER/PEM-encoded certificate.
  18. @retval FALSE It's NOT a DER/PEM-encoded certificate.
  19. **/
  20. BOOLEAN
  21. IsDerPemEncodeCertificate (
  22. IN CONST CHAR16 *FileSuffix
  23. )
  24. {
  25. UINTN Index;
  26. for (Index = 0; mDerPemEncodedSuffix[Index] != NULL; Index++) {
  27. if (StrCmp (FileSuffix, mDerPemEncodedSuffix[Index]) == 0) {
  28. return TRUE;
  29. }
  30. }
  31. return FALSE;
  32. }
  33. /**
  34. Read file content into BufferPtr, the size of the allocate buffer
  35. is *FileSize plus AddtionAllocateSize.
  36. @param[in] FileHandle The file to be read.
  37. @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
  38. @param[out] FileSize Size of input file
  39. @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
  40. In case the buffer need to contain others besides the file content.
  41. @retval EFI_SUCCESS The file was read into the buffer.
  42. @retval EFI_INVALID_PARAMETER A parameter was invalid.
  43. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  44. @retval others Unexpected error.
  45. **/
  46. EFI_STATUS
  47. ReadFileContent (
  48. IN EFI_FILE_HANDLE FileHandle,
  49. IN OUT VOID **BufferPtr,
  50. OUT UINTN *FileSize,
  51. IN UINTN AddtionAllocateSize
  52. )
  53. {
  54. UINTN BufferSize;
  55. UINT64 SourceFileSize;
  56. VOID *Buffer;
  57. EFI_STATUS Status;
  58. if ((FileHandle == NULL) || (FileSize == NULL)) {
  59. return EFI_INVALID_PARAMETER;
  60. }
  61. Buffer = NULL;
  62. //
  63. // Get the file size
  64. //
  65. Status = FileHandle->SetPosition (FileHandle, (UINT64)-1);
  66. if (EFI_ERROR (Status)) {
  67. goto ON_EXIT;
  68. }
  69. Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);
  70. if (EFI_ERROR (Status)) {
  71. goto ON_EXIT;
  72. }
  73. Status = FileHandle->SetPosition (FileHandle, 0);
  74. if (EFI_ERROR (Status)) {
  75. goto ON_EXIT;
  76. }
  77. BufferSize = (UINTN)SourceFileSize + AddtionAllocateSize;
  78. Buffer = AllocateZeroPool (BufferSize);
  79. if (Buffer == NULL) {
  80. return EFI_OUT_OF_RESOURCES;
  81. }
  82. BufferSize = (UINTN)SourceFileSize;
  83. *FileSize = BufferSize;
  84. Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);
  85. if (EFI_ERROR (Status) || (BufferSize != *FileSize)) {
  86. FreePool (Buffer);
  87. Buffer = NULL;
  88. Status = EFI_BAD_BUFFER_SIZE;
  89. goto ON_EXIT;
  90. }
  91. ON_EXIT:
  92. *BufferPtr = Buffer;
  93. return Status;
  94. }
  95. /**
  96. This function converts an input device structure to a Unicode string.
  97. @param[in] DevPath A pointer to the device path structure.
  98. @return A new allocated Unicode string that represents the device path.
  99. **/
  100. CHAR16 *
  101. EFIAPI
  102. DevicePathToStr (
  103. IN EFI_DEVICE_PATH_PROTOCOL *DevPath
  104. )
  105. {
  106. return ConvertDevicePathToText (
  107. DevPath,
  108. FALSE,
  109. TRUE
  110. );
  111. }
  112. /**
  113. Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
  114. The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
  115. means not enough memory resource.
  116. @param DevicePath Device path.
  117. @retval NULL Not enough memory resourece for AllocateCopyPool.
  118. @retval Other A new allocated string that represents the file name.
  119. **/
  120. CHAR16 *
  121. ExtractFileNameFromDevicePath (
  122. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
  123. )
  124. {
  125. CHAR16 *String;
  126. CHAR16 *MatchString;
  127. CHAR16 *LastMatch;
  128. CHAR16 *FileName;
  129. UINTN Length;
  130. ASSERT (DevicePath != NULL);
  131. String = DevicePathToStr (DevicePath);
  132. if (String == NULL) {
  133. return NULL;
  134. }
  135. MatchString = String;
  136. LastMatch = String;
  137. FileName = NULL;
  138. while (MatchString != NULL) {
  139. LastMatch = MatchString + 1;
  140. MatchString = StrStr (LastMatch, L"\\");
  141. }
  142. Length = StrLen (LastMatch);
  143. FileName = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), LastMatch);
  144. if (FileName != NULL) {
  145. *(FileName + Length) = 0;
  146. }
  147. FreePool (String);
  148. return FileName;
  149. }
  150. /**
  151. Update the form base on the selected file.
  152. @param[in] Private The pointer to the global private data structure.
  153. @param[in] FilePath Point to the file path.
  154. @param[in] FormId The form needs to display.
  155. @retval TRUE Exit caller function.
  156. @retval FALSE Not exit caller function.
  157. **/
  158. BOOLEAN
  159. UpdatePage (
  160. IN WIFI_MGR_PRIVATE_DATA *Private,
  161. IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
  162. IN EFI_FORM_ID FormId
  163. )
  164. {
  165. CHAR16 *FileName;
  166. EFI_STATUS Status;
  167. FileName = NULL;
  168. if (FilePath != NULL) {
  169. FileName = ExtractFileNameFromDevicePath (FilePath);
  170. }
  171. if (FileName == NULL) {
  172. //
  173. // FileName = NULL has two cases:
  174. // 1. FilePath == NULL, not select file.
  175. // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
  176. // In these two case, no need to update the form, and exit the caller function.
  177. //
  178. return TRUE;
  179. }
  180. //
  181. // Close the previous file handle before open a new one.
  182. //
  183. if (Private->FileContext->FHandle != NULL) {
  184. Private->FileContext->FHandle->Close (Private->FileContext->FHandle);
  185. }
  186. Private->FileContext->FHandle = NULL;
  187. Status = EfiOpenFileByDevicePath (
  188. &FilePath,
  189. &Private->FileContext->FHandle,
  190. EFI_FILE_MODE_READ,
  191. 0
  192. );
  193. if (EFI_ERROR (Status)) {
  194. if (FormId == FORMID_ENROLL_CERT) {
  195. HiiSetString (
  196. Private->RegisteredHandle,
  197. STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME),
  198. L"",
  199. NULL
  200. );
  201. } else if (FormId == FORMID_ENROLL_PRIVATE_KEY) {
  202. HiiSetString (
  203. Private->RegisteredHandle,
  204. STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME),
  205. L"",
  206. NULL
  207. );
  208. }
  209. } else {
  210. if (Private->FileContext->FileName != NULL) {
  211. FreePool (Private->FileContext->FileName);
  212. Private->FileContext->FileName = NULL;
  213. }
  214. Private->FileContext->FileName = FileName;
  215. if (FormId == FORMID_ENROLL_CERT) {
  216. HiiSetString (
  217. Private->RegisteredHandle,
  218. STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME),
  219. FileName,
  220. NULL
  221. );
  222. } else if (FormId == FORMID_ENROLL_PRIVATE_KEY) {
  223. HiiSetString (
  224. Private->RegisteredHandle,
  225. STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME),
  226. FileName,
  227. NULL
  228. );
  229. }
  230. }
  231. return TRUE;
  232. }
  233. /**
  234. Update the CA form base on the input file path info.
  235. @param[in] Private The pointer to the global private data structure.
  236. @param[in] FilePath Point to the file path.
  237. @retval TRUE Exit caller function.
  238. @retval FALSE Not exit caller function.
  239. **/
  240. BOOLEAN
  241. UpdateCAFromFile (
  242. IN WIFI_MGR_PRIVATE_DATA *Private,
  243. IN EFI_DEVICE_PATH_PROTOCOL *FilePath
  244. )
  245. {
  246. return UpdatePage (Private, FilePath, FORMID_ENROLL_CERT);
  247. }
  248. /**
  249. Update the Private Key form base on the input file path info.
  250. @param[in] Private The pointer to the global private data structure.
  251. @param[in] FilePath Point to the file path.
  252. @retval TRUE Exit caller function.
  253. @retval FALSE Not exit caller function.
  254. **/
  255. BOOLEAN
  256. UpdatePrivateKeyFromFile (
  257. IN WIFI_MGR_PRIVATE_DATA *Private,
  258. IN EFI_DEVICE_PATH_PROTOCOL *FilePath
  259. )
  260. {
  261. return UpdatePage (Private, FilePath, FORMID_ENROLL_PRIVATE_KEY);
  262. }