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