1 /** @file
2   The Miscellaneous Routines 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 "WifiConnectionMgrDxe.h"
11 
12 /**
13   Empty function for event process function.
14 
15   @param Event    The Event need to be process
16   @param Context  The context of the event.
17 
18 **/
19 VOID
20 EFIAPI
WifiMgrInternalEmptyFunction(IN EFI_EVENT Event,IN VOID * Context)21 WifiMgrInternalEmptyFunction (
22   IN  EFI_EVENT    Event,
23   IN  VOID         *Context
24   )
25 {
26   return;
27 }
28 
29 /**
30   Convert the mac address into a hexadecimal encoded ":" seperated string.
31 
32   @param[in]  Mac     The mac address.
33   @param[in]  StrSize The size, in bytes, of the output buffer specified by Str.
34   @param[out] Str     The storage to return the mac string.
35 
36 **/
37 VOID
WifiMgrMacAddrToStr(IN EFI_80211_MAC_ADDRESS * Mac,IN UINT32 StrSize,OUT CHAR16 * Str)38 WifiMgrMacAddrToStr (
39   IN  EFI_80211_MAC_ADDRESS  *Mac,
40   IN  UINT32                 StrSize,
41   OUT CHAR16                 *Str
42   )
43 {
44   if (Mac == NULL || Str == NULL) {
45     return;
46   }
47 
48   UnicodeSPrint (
49     Str,
50     StrSize,
51     L"%02X:%02X:%02X:%02X:%02X:%02X",
52     Mac->Addr[0], Mac->Addr[1], Mac->Addr[2],
53     Mac->Addr[3], Mac->Addr[4], Mac->Addr[5]
54     );
55 }
56 
57 /**
58   Read private key file to buffer.
59 
60   @param[in]   FileContext           The file context of private key file.
61   @param[out]  PrivateKeyDataAddr    The buffer address to restore private key file, should be
62                                      freed by caller.
63   @param[out]  PrivateKeyDataSize    The size of read private key file.
64 
65   @retval EFI_SUCCESS                Successfully read the private key file.
66   @retval EFI_INVALID_PARAMETER      One or more of the parameters is invalid.
67 
68 **/
69 EFI_STATUS
WifiMgrReadFileToBuffer(IN WIFI_MGR_FILE_CONTEXT * FileContext,OUT VOID ** DataAddr,OUT UINTN * DataSize)70 WifiMgrReadFileToBuffer (
71   IN   WIFI_MGR_FILE_CONTEXT          *FileContext,
72   OUT  VOID                           **DataAddr,
73   OUT  UINTN                          *DataSize
74   )
75 {
76   EFI_STATUS    Status;
77 
78   if (FileContext != NULL && FileContext->FHandle != NULL) {
79 
80     Status = ReadFileContent (
81                FileContext->FHandle,
82                DataAddr,
83                DataSize,
84                0
85                );
86 
87     if (FileContext->FHandle != NULL) {
88       FileContext->FHandle->Close (FileContext->FHandle);
89     }
90     FileContext->FHandle = NULL;
91     return Status;
92   }
93 
94   return EFI_INVALID_PARAMETER;
95 }
96 
97 /**
98   Get the Nic data by the NicIndex.
99 
100   @param[in]  Private        The pointer to the global private data structure.
101   @param[in]  NicIndex       The index indicates the position of wireless NIC.
102 
103   @return     Pointer to the Nic data, or NULL if not found.
104 
105 **/
106 WIFI_MGR_DEVICE_DATA *
WifiMgrGetNicByIndex(IN WIFI_MGR_PRIVATE_DATA * Private,IN UINT32 NicIndex)107 WifiMgrGetNicByIndex (
108   IN WIFI_MGR_PRIVATE_DATA   *Private,
109   IN UINT32                  NicIndex
110   )
111 {
112   LIST_ENTRY             *Entry;
113   WIFI_MGR_DEVICE_DATA   *Nic;
114 
115   if (Private == NULL) {
116     return NULL;
117   }
118 
119   NET_LIST_FOR_EACH (Entry, &Private->NicList) {
120     Nic = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_DEVICE_DATA,
121             Link, WIFI_MGR_DEVICE_DATA_SIGNATURE);
122     if (Nic->NicIndex == NicIndex) {
123       return Nic;
124     }
125   }
126 
127   return NULL;
128 }
129 
130 /**
131   Find a network profile through its' SSId and securit type, and the SSId is an unicode string.
132 
133   @param[in]  SSId                   The target network's SSId.
134   @param[in]  SecurityType           The target network's security type.
135   @param[in]  ProfileList            The profile list on a Nic.
136 
137   @return Pointer to a network profile, or NULL if not found.
138 
139 **/
140 WIFI_MGR_NETWORK_PROFILE *
WifiMgrGetProfileByUnicodeSSId(IN CHAR16 * SSId,IN UINT8 SecurityType,IN LIST_ENTRY * ProfileList)141 WifiMgrGetProfileByUnicodeSSId (
142   IN  CHAR16                         *SSId,
143   IN  UINT8                          SecurityType,
144   IN  LIST_ENTRY                     *ProfileList
145   )
146 {
147   LIST_ENTRY                         *Entry;
148   WIFI_MGR_NETWORK_PROFILE           *Profile;
149 
150   if (SSId == NULL || ProfileList == NULL) {
151     return NULL;
152   }
153 
154   NET_LIST_FOR_EACH (Entry, ProfileList) {
155     Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE,
156                 Link, WIFI_MGR_PROFILE_SIGNATURE);
157     if (StrCmp (SSId, Profile->SSId) == 0 && SecurityType == Profile->SecurityType) {
158       return Profile;
159     }
160   }
161 
162   return NULL;
163 }
164 
165 /**
166   Find a network profile through its' SSId and securit type, and the SSId is an ascii string.
167 
168   @param[in]  SSId               The target network's SSId.
169   @param[in]  SecurityType       The target network's security type.
170   @param[in]  ProfileList        The profile list on a Nic.
171 
172   @return Pointer to a network profile, or NULL if not found.
173 
174 **/
175 WIFI_MGR_NETWORK_PROFILE *
WifiMgrGetProfileByAsciiSSId(IN CHAR8 * SSId,IN UINT8 SecurityType,IN LIST_ENTRY * ProfileList)176 WifiMgrGetProfileByAsciiSSId (
177   IN  CHAR8                      *SSId,
178   IN  UINT8                      SecurityType,
179   IN  LIST_ENTRY                 *ProfileList
180   )
181 {
182   CHAR16   SSIdUniCode[SSID_STORAGE_SIZE];
183 
184   if (SSId == NULL) {
185     return NULL;
186   }
187   if (AsciiStrToUnicodeStrS (SSId, SSIdUniCode, SSID_STORAGE_SIZE) != RETURN_SUCCESS) {
188     return NULL;
189   }
190 
191   return WifiMgrGetProfileByUnicodeSSId (SSIdUniCode, SecurityType, ProfileList);
192 }
193 
194 /**
195   Find a network profile through its' profile index.
196 
197   @param[in]  ProfileIndex           The target network's profile index.
198   @param[in]  ProfileList            The profile list on a Nic.
199 
200   @return Pointer to a network profile, or NULL if not found.
201 
202 **/
203 WIFI_MGR_NETWORK_PROFILE *
WifiMgrGetProfileByProfileIndex(IN UINT32 ProfileIndex,IN LIST_ENTRY * ProfileList)204 WifiMgrGetProfileByProfileIndex (
205   IN  UINT32                         ProfileIndex,
206   IN  LIST_ENTRY                     *ProfileList
207   )
208 {
209   WIFI_MGR_NETWORK_PROFILE           *Profile;
210   LIST_ENTRY                         *Entry;
211 
212   if (ProfileList == NULL) {
213     return NULL;
214   }
215   NET_LIST_FOR_EACH (Entry, ProfileList) {
216     Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE,
217                 Link, WIFI_MGR_PROFILE_SIGNATURE);
218     if (Profile->ProfileIndex == ProfileIndex) {
219       return Profile;
220     }
221   }
222   return NULL;
223 }
224 
225 /**
226   To test if the AKMSuite is in supported AKMSuite list.
227 
228   @param[in]  SupportedAKMSuiteCount       The count of the supported AKMSuites.
229   @param[in]  SupportedAKMSuiteList        The supported AKMSuite list.
230   @param[in]  AKMSuite                     The AKMSuite to be tested.
231 
232   @return True if this AKMSuite is supported, or False if not.
233 
234 **/
235 BOOLEAN
WifiMgrSupportAKMSuite(IN UINT16 SupportedAKMSuiteCount,IN UINT32 * SupportedAKMSuiteList,IN UINT32 * AKMSuite)236 WifiMgrSupportAKMSuite (
237   IN  UINT16                               SupportedAKMSuiteCount,
238   IN  UINT32                               *SupportedAKMSuiteList,
239   IN  UINT32                               *AKMSuite
240   )
241 {
242   UINT16    Index;
243 
244   if (AKMSuite == NULL || SupportedAKMSuiteList == NULL ||
245     SupportedAKMSuiteCount == 0) {
246     return FALSE;
247   }
248 
249   for (Index = 0; Index < SupportedAKMSuiteCount; Index ++) {
250     if (SupportedAKMSuiteList[Index] == *AKMSuite) {
251       return TRUE;
252     }
253   }
254 
255   return FALSE;
256 }
257 
258 /**
259   To check if the CipherSuite is in supported CipherSuite list.
260 
261   @param[in]  SupportedCipherSuiteCount       The count of the supported CipherSuites.
262   @param[in]  SupportedCipherSuiteList        The supported CipherSuite list.
263   @param[in]  CipherSuite                     The CipherSuite to be tested.
264 
265   @return True if this CipherSuite is supported, or False if not.
266 
267 **/
268 BOOLEAN
WifiMgrSupportCipherSuite(IN UINT16 SupportedCipherSuiteCount,IN UINT32 * SupportedCipherSuiteList,IN UINT32 * CipherSuite)269 WifiMgrSupportCipherSuite (
270   IN  UINT16                                  SupportedCipherSuiteCount,
271   IN  UINT32                                  *SupportedCipherSuiteList,
272   IN  UINT32                                  *CipherSuite
273   )
274 {
275   UINT16  Index;
276 
277   if (CipherSuite == NULL || SupportedCipherSuiteCount == 0 ||
278     SupportedCipherSuiteList == NULL) {
279     return FALSE;
280   }
281 
282   for (Index = 0; Index < SupportedCipherSuiteCount; Index ++) {
283     if (SupportedCipherSuiteList[Index] == *CipherSuite) {
284       return TRUE;
285     }
286   }
287 
288   return FALSE;
289 }
290 
291 /**
292   Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites
293   are supported and find the matchable security type.
294 
295   @param[in]   AKMList                     The target AKM suite list to be checked.
296   @param[in]   CipherList                  The target Cipher suite list to be checked
297   @param[in]   Nic                         The Nic to operate, contains the supported AKMSuite list
298                                            and supported CipherSuite list
299   @param[out]  SecurityType                To identify a security type from the AKM suite list and
300                                            Cipher suite list
301   @param[out]  AKMSuiteSupported           To identify if this security type is supported. If it is
302                                            NULL, overcome this field
303   @param[out]  CipherSuiteSupported        To identify if this security type is supported. If it is
304                                            NULL, overcome this field
305 
306   @retval EFI_SUCCESS                      This operation has completed successfully.
307   @retval EFI_INVALID_PARAMETER            No Nic found or the suite list is null.
308 
309 **/
310 EFI_STATUS
WifiMgrCheckRSN(IN EFI_80211_AKM_SUITE_SELECTOR * AKMList,IN EFI_80211_CIPHER_SUITE_SELECTOR * CipherList,IN WIFI_MGR_DEVICE_DATA * Nic,OUT UINT8 * SecurityType,OUT BOOLEAN * AKMSuiteSupported,OUT BOOLEAN * CipherSuiteSupported)311 WifiMgrCheckRSN (
312   IN    EFI_80211_AKM_SUITE_SELECTOR       *AKMList,
313   IN    EFI_80211_CIPHER_SUITE_SELECTOR    *CipherList,
314   IN    WIFI_MGR_DEVICE_DATA               *Nic,
315   OUT   UINT8                              *SecurityType,
316   OUT   BOOLEAN                            *AKMSuiteSupported,
317   OUT   BOOLEAN                            *CipherSuiteSupported
318   )
319 {
320   EFI_80211_AKM_SUITE_SELECTOR             *SupportedAKMSuites;
321   EFI_80211_CIPHER_SUITE_SELECTOR          *SupportedSwCipherSuites;
322   EFI_80211_CIPHER_SUITE_SELECTOR          *SupportedHwCipherSuites;
323   EFI_80211_SUITE_SELECTOR                 *AKMSuite;
324   EFI_80211_SUITE_SELECTOR                 *CipherSuite;
325   UINT16                                   AKMIndex;
326   UINT16                                   CipherIndex;
327 
328   if (Nic == NULL || AKMList == NULL || CipherList == NULL|| SecurityType == NULL) {
329     return EFI_INVALID_PARAMETER;
330   }
331 
332   SupportedAKMSuites      = Nic->SupportedSuites.SupportedAKMSuites;
333   SupportedSwCipherSuites = Nic->SupportedSuites.SupportedSwCipherSuites;
334   SupportedHwCipherSuites = Nic->SupportedSuites.SupportedHwCipherSuites;
335 
336   *SecurityType = SECURITY_TYPE_UNKNOWN;
337   if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) {
338     *AKMSuiteSupported    = FALSE;
339     *CipherSuiteSupported = FALSE;
340   }
341 
342   if (AKMList->AKMSuiteCount == 0) {
343     if (CipherList->CipherSuiteCount == 0) {
344       *SecurityType = SECURITY_TYPE_NONE;
345       if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) {
346         *AKMSuiteSupported    = TRUE;
347         *CipherSuiteSupported = TRUE;
348       }
349     }
350 
351     return EFI_SUCCESS;
352   }
353 
354   for (AKMIndex = 0; AKMIndex < AKMList->AKMSuiteCount; AKMIndex ++) {
355 
356     AKMSuite = AKMList->AKMSuiteList + AKMIndex;
357     if (WifiMgrSupportAKMSuite(SupportedAKMSuites->AKMSuiteCount,
358       (UINT32*) SupportedAKMSuites->AKMSuiteList, (UINT32*) AKMSuite)) {
359 
360       if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) {
361         *AKMSuiteSupported = TRUE;
362       }
363       for (CipherIndex = 0; CipherIndex < CipherList->CipherSuiteCount; CipherIndex ++) {
364 
365         CipherSuite = CipherList->CipherSuiteList + CipherIndex;
366 
367         if (SupportedSwCipherSuites != NULL) {
368 
369           if (WifiMgrSupportCipherSuite(SupportedSwCipherSuites->CipherSuiteCount,
370             (UINT32*) SupportedSwCipherSuites->CipherSuiteList, (UINT32*) CipherSuite)) {
371 
372             *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMSuite, (UINT32*) CipherSuite);
373 
374             if (*SecurityType != SECURITY_TYPE_UNKNOWN) {
375 
376               if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) {
377                 *CipherSuiteSupported = TRUE;
378               }
379               return EFI_SUCCESS;
380             }
381           }
382         }
383 
384         if (SupportedHwCipherSuites != NULL) {
385 
386           if (WifiMgrSupportCipherSuite(SupportedHwCipherSuites->CipherSuiteCount,
387             (UINT32*) SupportedHwCipherSuites->CipherSuiteList, (UINT32*) CipherSuite)) {
388 
389             *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMSuite, (UINT32*) CipherSuite);
390 
391             if (*SecurityType != SECURITY_TYPE_UNKNOWN) {
392 
393               if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) {
394                 *CipherSuiteSupported = TRUE;
395               }
396               return EFI_SUCCESS;
397             }
398           }
399         }
400       }
401     }
402   }
403 
404   *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMList->AKMSuiteList,
405                     (UINT32*) CipherList->CipherSuiteList);
406 
407   return EFI_SUCCESS;
408 }
409 
410 /**
411   Get the security type for a certain AKMSuite and CipherSuite.
412 
413   @param[in]   AKMSuite             An certain AKMSuite.
414   @param[in]   CipherSuite          An certain CipherSuite.
415 
416   @return a security type if found, or SECURITY_TYPE_UNKNOWN.
417 
418 **/
419 UINT8
WifiMgrGetSecurityType(IN UINT32 * AKMSuite,IN UINT32 * CipherSuite)420 WifiMgrGetSecurityType (
421   IN  UINT32    *AKMSuite,
422   IN  UINT32    *CipherSuite
423   )
424 {
425   if (CipherSuite == NULL) {
426 
427     if (AKMSuite == NULL) {
428       return SECURITY_TYPE_NONE;
429     } else {
430       return SECURITY_TYPE_UNKNOWN;
431     }
432   } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP) {
433 
434     if (AKMSuite == NULL) {
435       return SECURITY_TYPE_NONE;
436     } else {
437       return SECURITY_TYPE_UNKNOWN;
438     }
439   } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40 ||
440     *CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104) {
441 
442     return SECURITY_TYPE_WEP;
443   } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP) {
444 
445     if (AKMSuite == NULL) {
446       return SECURITY_TYPE_UNKNOWN;
447     }
448 
449     if (*AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA ||
450       *AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256) {
451 
452       return SECURITY_TYPE_WPA2_ENTERPRISE;
453     } else if (*AKMSuite == IEEE_80211_AKM_SUITE_PSK ||
454         *AKMSuite == IEEE_80211_AKM_SUITE_PSK_SHA256){
455 
456       return SECURITY_TYPE_WPA2_PERSONAL;
457     }else {
458       return SECURITY_TYPE_UNKNOWN;
459     }
460   } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP) {
461 
462     if (AKMSuite == NULL) {
463       return SECURITY_TYPE_UNKNOWN;
464     }
465 
466     if (*AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA ||
467       *AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256) {
468 
469       return SECURITY_TYPE_WPA_ENTERPRISE;
470     } else if (*AKMSuite == IEEE_80211_AKM_SUITE_PSK ||
471         *AKMSuite == IEEE_80211_AKM_SUITE_PSK_SHA256){
472 
473       return SECURITY_TYPE_WPA_PERSONAL;
474     }else {
475       return SECURITY_TYPE_UNKNOWN;
476     }
477   } else {
478     return SECURITY_TYPE_UNKNOWN;
479   }
480 }
481 
482 /**
483   Get supported AKMSuites and CipherSuites from supplicant for a Nic.
484 
485   @param[in]   Nic                      The Nic to operate.
486 
487   @retval EFI_SUCCESS                   Get the supported suite list successfully.
488   @retval EFI_INVALID_PARAMETER         No Nic found or supplicant is NULL.
489 
490 **/
491 EFI_STATUS
WifiMgrGetSupportedSuites(IN WIFI_MGR_DEVICE_DATA * Nic)492 WifiMgrGetSupportedSuites (
493   IN    WIFI_MGR_DEVICE_DATA            *Nic
494   )
495 {
496   EFI_STATUS                            Status;
497   EFI_SUPPLICANT_PROTOCOL               *Supplicant;
498   EFI_80211_AKM_SUITE_SELECTOR          *SupportedAKMSuites;
499   EFI_80211_CIPHER_SUITE_SELECTOR       *SupportedSwCipherSuites;
500   EFI_80211_CIPHER_SUITE_SELECTOR       *SupportedHwCipherSuites;
501   UINTN                                 DataSize;
502 
503   SupportedAKMSuites      = NULL;
504   SupportedSwCipherSuites = NULL;
505   SupportedHwCipherSuites = NULL;
506 
507   if (Nic == NULL || Nic->Supplicant == NULL) {
508     return EFI_INVALID_PARAMETER;
509   }
510 
511   Supplicant = Nic->Supplicant;
512 
513   DataSize  = 0;
514   Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedAKMSuites, NULL, &DataSize);
515   if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) {
516 
517     SupportedAKMSuites = AllocateZeroPool(DataSize);
518     if (SupportedAKMSuites == NULL) {
519       return EFI_OUT_OF_RESOURCES;
520     }
521     Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedAKMSuites,
522                            (UINT8 *) SupportedAKMSuites, &DataSize);
523     if (!EFI_ERROR (Status)) {
524       Nic->SupportedSuites.SupportedAKMSuites = SupportedAKMSuites;
525     } else {
526       FreePool (SupportedAKMSuites);
527     }
528   } else {
529     SupportedAKMSuites = NULL;
530   }
531 
532   DataSize  = 0;
533   Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedSoftwareCipherSuites, NULL, &DataSize);
534   if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) {
535 
536 
537     SupportedSwCipherSuites = AllocateZeroPool(DataSize);
538     if (SupportedSwCipherSuites == NULL) {
539       return EFI_OUT_OF_RESOURCES;
540     }
541     Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedSoftwareCipherSuites,
542                            (UINT8 *) SupportedSwCipherSuites, &DataSize);
543     if (!EFI_ERROR (Status)) {
544       Nic->SupportedSuites.SupportedSwCipherSuites = SupportedSwCipherSuites;
545     } else {
546       FreePool (SupportedSwCipherSuites);
547     }
548   } else {
549     SupportedSwCipherSuites = NULL;
550   }
551 
552   DataSize  = 0;
553   Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedHardwareCipherSuites, NULL, &DataSize);
554   if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) {
555 
556     SupportedHwCipherSuites = AllocateZeroPool(DataSize);
557     if (SupportedHwCipherSuites == NULL) {
558       return EFI_OUT_OF_RESOURCES;
559     }
560     Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedHardwareCipherSuites,
561                            (UINT8 *) SupportedHwCipherSuites, &DataSize);
562     if (!EFI_ERROR (Status)) {
563       Nic->SupportedSuites.SupportedHwCipherSuites = SupportedHwCipherSuites;
564     } else {
565       FreePool (SupportedHwCipherSuites);
566     }
567   } else {
568     SupportedHwCipherSuites = NULL;
569   }
570 
571   return EFI_SUCCESS;
572 }
573 
574 /**
575   Clean secrets from a network profile.
576 
577   @param[in]   Profile               The profile to be cleanned.
578 
579 **/
580 VOID
WifiMgrCleanProfileSecrets(IN WIFI_MGR_NETWORK_PROFILE * Profile)581 WifiMgrCleanProfileSecrets (
582   IN  WIFI_MGR_NETWORK_PROFILE       *Profile
583   )
584 {
585   ZeroMem (Profile->Password, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE);
586   ZeroMem (Profile->EapPassword, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE);
587   ZeroMem (Profile->PrivateKeyPassword, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE);
588 
589   if (Profile->CACertData != NULL) {
590 
591     ZeroMem (Profile->CACertData, Profile->CACertSize);
592     FreePool (Profile->CACertData);
593   }
594   Profile->CACertData = NULL;
595   Profile->CACertSize = 0;
596 
597   if (Profile->ClientCertData != NULL) {
598 
599     ZeroMem (Profile->ClientCertData, Profile->ClientCertSize);
600     FreePool (Profile->ClientCertData);
601   }
602   Profile->ClientCertData = NULL;
603   Profile->ClientCertSize = 0;
604 
605   if (Profile->PrivateKeyData != NULL) {
606 
607     ZeroMem (Profile->PrivateKeyData, Profile->PrivateKeyDataSize);
608     FreePool (Profile->PrivateKeyData);
609   }
610   Profile->PrivateKeyData     = NULL;
611   Profile->PrivateKeyDataSize = 0;
612 }
613 
614 /**
615   Free all network profiles in a profile list.
616 
617   @param[in]   ProfileList           The profile list to be freed.
618 
619 **/
620 VOID
WifiMgrFreeProfileList(IN LIST_ENTRY * ProfileList)621 WifiMgrFreeProfileList (
622   IN  LIST_ENTRY                     *ProfileList
623   )
624 {
625   WIFI_MGR_NETWORK_PROFILE           *Profile;
626   LIST_ENTRY                         *Entry;
627   LIST_ENTRY                         *NextEntry;
628 
629   if (ProfileList == NULL) {
630     return;
631   }
632 
633   NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, ProfileList) {
634 
635     Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE,
636                 Link, WIFI_MGR_PROFILE_SIGNATURE);
637 
638     WifiMgrCleanProfileSecrets (Profile);
639 
640     if (Profile->Network.AKMSuite != NULL) {
641       FreePool(Profile->Network.AKMSuite);
642     }
643 
644     if (Profile->Network.CipherSuite != NULL) {
645       FreePool(Profile->Network.CipherSuite);
646     }
647 
648     FreePool (Profile);
649   }
650 }
651 
652 /**
653   Free user configured hidden network list.
654 
655   @param[in]   HiddenList           The hidden network list to be freed.
656 
657 **/
658 VOID
WifiMgrFreeHiddenList(IN LIST_ENTRY * HiddenList)659 WifiMgrFreeHiddenList (
660   IN  LIST_ENTRY                     *HiddenList
661   )
662 {
663   WIFI_HIDDEN_NETWORK_DATA           *HiddenNetwork;
664   LIST_ENTRY                         *Entry;
665   LIST_ENTRY                         *NextEntry;
666 
667   if (HiddenList == NULL) {
668     return;
669   }
670 
671   NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, HiddenList) {
672 
673     HiddenNetwork = NET_LIST_USER_STRUCT_S (Entry, WIFI_HIDDEN_NETWORK_DATA,
674                       Link, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE);
675     FreePool (HiddenNetwork);
676   }
677 }
678 
679 
680 /**
681   Free the resources of a config token.
682 
683   @param[in]   ConfigToken          The config token to be freed.
684 **/
685 VOID
WifiMgrFreeToken(IN WIFI_MGR_MAC_CONFIG_TOKEN * ConfigToken)686 WifiMgrFreeToken (
687   IN   WIFI_MGR_MAC_CONFIG_TOKEN    *ConfigToken
688   )
689 {
690   EFI_80211_GET_NETWORKS_RESULT     *Result;
691 
692   if (ConfigToken == NULL) {
693     return;
694   }
695 
696   switch (ConfigToken->Type) {
697 
698     case TokenTypeGetNetworksToken:
699 
700       if (ConfigToken->Token.GetNetworksToken != NULL) {
701 
702         gBS->CloseEvent (ConfigToken->Token.GetNetworksToken->Event);
703         if (ConfigToken->Token.GetNetworksToken->Data != NULL) {
704           FreePool(ConfigToken->Token.GetNetworksToken->Data);
705         }
706 
707         Result = ConfigToken->Token.GetNetworksToken->Result;
708         if (Result != NULL) {
709           FreePool (Result);
710         }
711 
712         FreePool(ConfigToken->Token.GetNetworksToken);
713       }
714 
715       FreePool (ConfigToken);
716       break;
717 
718     case TokenTypeConnectNetworkToken:
719 
720       if (ConfigToken->Token.ConnectNetworkToken != NULL) {
721 
722         gBS->CloseEvent (ConfigToken->Token.ConnectNetworkToken->Event);
723         if (ConfigToken->Token.ConnectNetworkToken->Data != NULL) {
724           FreePool(ConfigToken->Token.ConnectNetworkToken->Data);
725         }
726         FreePool(ConfigToken->Token.ConnectNetworkToken);
727       }
728       FreePool (ConfigToken);
729       break;
730 
731     case TokenTypeDisconnectNetworkToken:
732 
733       if (ConfigToken->Token.DisconnectNetworkToken != NULL) {
734 
735         FreePool(ConfigToken->Token.DisconnectNetworkToken);
736       }
737 
738       FreePool (ConfigToken);
739       break;
740 
741     default :
742       break;
743   }
744 }
745