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