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