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